发送Http Get和Post请求

发送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();

 

posted @ 2014-08-23 22:21    阅读(401)  评论(0编辑  收藏  举报