网络请求帮助类 C#
>>> Get请求 <<<
/// <summary>
/// 请求路径
/// </summary>
/// <param name="Url">请求的路径</param>
/// <returns>返回请求的网页数据</returns>
public static string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
return retString;
}
catch (Exception e)
{
throw e;
}
}
>>> Post请求 <<<
/// <summary>
/// 提交数据
/// </summary>
/// <param name="Url">提交的路径</param>
/// <param name="Data">提交的数据</param>
/// <returns>返回结果</returns>
public static string HttpPost(string Url, string Data)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.Referer = Url;
request.ContentLength = Encoding.UTF8.GetByteCount(Data);
request.CookieContainer = cookie;
request.Method = "POST";
Stream myRequestStream = request.GetRequestStream();
byte[] postBytes = Encoding.UTF8.GetBytes(Data);
myRequestStream.Write(postBytes, 0, postBytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = cookie.GetCookies(response.ResponseUri);
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
return retString;
}
catch (Exception ex)
{
throw ex;
}
}
封装的类: HttpHelp.cs
版权属于:zgcwkj
本文链接:https://blog.zgcwkj.cn/archives/26.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭