码迷,mamicode.com
首页 > 其他好文 > 详细

发送Http Get和Post请求

时间:2014-08-23 22:55:11      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   strong   for   div   

发送Get请求

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";

req.Timeout = config.Timeout;//设置超时时间

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
responseData = sr.ReadToEnd();
sr.Close();
res.Close();

 

 

 

发送Post请求

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["Timeout"]);//设置超时时间

req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";

byte[] sendData = Encoding.UTF8.GetBytes(data);
req.ContentLength = sendData.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(sendData, 0, sendData.Length);
newStream.Close();

// Get response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
responseData = sr.ReadToEnd();
sr.Close();
res.Close();

 

发送Http Get和Post请求

标签:style   blog   http   color   os   io   strong   for   div   

原文地址:http://www.cnblogs.com/gossip/p/3919384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!