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 }