C#POST数据,HttpWebRequest请求页面,HttpWebResponse返回数据
Posted on 2010-05-21 17:45 RockyYu 阅读(3988) 评论(0) 编辑 收藏 举报C#调用页面POST数据,并得到返回内容的代码:
代码
1 private string GetResponseData(byte[] postData)
2 {
3 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://XXX.XXX.XXX");
4 myRequest.Method = "POST";
5 myRequest.ContentType = "application/x-www-form-urlencoded";
6 myRequest.ContentLength = postData.Length;
7
8 Stream newStream = myRequest.GetRequestStream();
9 // Send the data.
10 newStream.Write(postData, 0, postData.Length);
11 newStream.Close();
12
13 // Get response
14 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
15 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
16 return reader.ReadToEnd();
17 }
2 {
3 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://XXX.XXX.XXX");
4 myRequest.Method = "POST";
5 myRequest.ContentType = "application/x-www-form-urlencoded";
6 myRequest.ContentLength = postData.Length;
7
8 Stream newStream = myRequest.GetRequestStream();
9 // Send the data.
10 newStream.Write(postData, 0, postData.Length);
11 newStream.Close();
12
13 // Get response
14 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
15 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
16 return reader.ReadToEnd();
17 }
--小小的天,有大大的梦想!