首页 > 代码库 > 发送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请求
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。