/// <summary>
/// 使用Get方法获取字符串结果(没有加入Cookie)
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string HttpGet(string url, Encoding encoding = null)
{
WebClient wc = new WebClient();
wc.Encoding = encoding ?? Encoding.UTF8;
//if (encoding != null)
//{
// wc.Encoding = encoding;
//}
return wc.DownloadString(url);
}
//body是要传递的参数,格式"roleId=1&uid=2"
//post的cotentType填写:
//"application/x-www-form-urlencoded"
//soap填写:"text/xml; charset=utf-8"
public static string PostHttp(string url, string body, string contentType)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = contentType;
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 20000;
byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return responseContent;
}
C# 后台 POST GET 使用的API
最新推荐文章于 2024-08-26 10:34:03 发布