/// <summary>
/// 调用http请求
/// </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);//创建一个stream读取流
string msgs = sr.ReadToEnd();
sr.Close();
return msgs;
}
catch (Exception error)
{
throw new Exception(String.Format("请求方法{0}时出现异常:", cReferralUrl) + error.Message + Environment.NewLine + "入参:" + postPar);
}
}
c#调用HTTP请求
最新推荐文章于 2024-08-08 20:28:25 发布