HTTP GET 请求代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baidu.com/"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); string responseData = readStream.ReadToEnd(); response.Close(); readStream.Close();
Response.Write(responseData);
HTTP POST请求代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"); string data = "byProvinceName=河北"; byte[] bs = Encoding.UTF8.GetBytes(data); string responseData; request.Method = "POST"; request.ContentLength = bs.Length; request.ContentType = "application/x-www-form-urlencoded"; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseData = readStream.ReadToEnd().ToString(); readStream.Close(); } response.Close(); } Response.Write(responseData);