现在的位置: 首页 > 综合 > 正文

c#使用post,get请求网页

2013年12月02日 ⁄ 综合 ⁄ 共 1037字 ⁄ 字号 评论关闭

 

get方式:

string url = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("gb2312"));
MessageBox.Show(sr.ReadToEnd());

sr.Dispose();
sr.Close();
s.Dispose();
s.Close();

post方式:

string url = http://maps.googleapis.com/maps/api/geocode/json;
string postData = "address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";

WebRequest request = WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(postData);
sw.Flush();

WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("gb2312"));
MessageBox.Show(sr.ReadToEnd());

sw.Dispose();
sw.Close();
sr.Dispose();
sr.Close();
s.Dispose();
s.Close();

 

抱歉!评论已关闭.