C#中使用post请求方法请求表单-用于两个网站的交互
网站A中的后台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //请求的URL 2 HttpWebRequest request = WebRequest.Create("http://192.168.1.132:86/test01.aspx") as HttpWebRequest; 3 request.Method = "post"; 4 5 string data = "传输的数据"; 6 //List<string> list = new List<string>(data); 7 //string newdata = jsonHelper.GetJSON<List<string>> (list); 8 byte[] bytes = Encoding.Default.GetBytes(data); 9 request.ContentLength = bytes.Length; 10 request.ContentType = "application/x-www-form-urlencoded"; 11 Stream sw = request.GetRequestStream(); 12 sw.Write(bytes, 0, bytes.Length); 13 sw.Close(); 14 15 HttpWebResponse resp = request.GetResponse() as HttpWebResponse; 16 Stream st = resp.GetResponseStream(); 17 StreamReader sr = new StreamReader(st); 18 string returnValue = sr.ReadToEnd(); 19 sr.Close(); 20 Response.Write(returnValue);
网站B中的后台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 if (Request.HttpMethod.ToUpper()=="POST") 2 { 3 Stream input =Request.InputStream; 4 StreamReader sr = new System.IO.StreamReader(input,Encoding.Default); 5 string s=sr.ReadToEnd(); 6 sr.Close(); 7 sr.Dispose(); 8 9 Response.Write("<script>alert('你输入的是:"+s+"');</script>"); 10 Response.End(); 11 }