c#调用HTTP请求
生活随笔
收集整理的這篇文章主要介紹了
c#调用HTTP请求
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/// <summary>/// 調(diào)用http請(qǐng)求/// </summary>/// <param name="postPar"></param>/// <returns></returns>public string HttpRequest(string postMethod, string postPar){string cReferralUrl = DrSofts.Clients.Dbs.Data.GetValueFromSystemSet("cReferralUrl");cReferralUrl = "http://" + cReferralUrl + postMethod;try{HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(cReferralUrl);hwr.Accept = "text/html, application/xhtml+xml, */*";hwr.ContentType = "application/x-www-form-urlencoded";hwr.Method = "POST";hwr.KeepAlive = true;hwr.ReadWriteTimeout = 3000;byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postPar.ToString());using (Stream stream = hwr.GetRequestStream()){stream.Write(data, 0, data.Length);stream.Close();}Stream stre = hwr.GetResponse().GetResponseStream();StreamReader sr = new StreamReader(stre);//創(chuàng)建一個(gè)stream讀取流string msgs = sr.ReadToEnd();sr.Close();return msgs;}catch (Exception error){throw new Exception(String.Format("請(qǐng)求方法{0}時(shí)出現(xiàn)異常:", cReferralUrl) + error.Message + Environment.NewLine + "入?yún)?#xff1a;" + postPar);}}
總結(jié)
以上是生活随笔為你收集整理的c#调用HTTP请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Task01c:随机抽样与卡方检验的SQ
- 下一篇: 【AC自动机+DP】USACO2012