首页 > 代码库 > 发送Http Get和Post请求

发送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 =http://www.mamicode.com/ Encoding.UTF8.GetBytes(data);req.ContentLength = sendData.Length;Stream newStream = req.GetRequestStream();newStream.Write(sendData, 0, sendData.Length);newStream.Close();// Get responseHttpWebResponse res = (HttpWebResponse)req.GetResponse();StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);responseData = sr.ReadToEnd();sr.Close();res.Close();

 

发送Http Get和Post请求