C# HttpWebRequest访问网页通用封装代码实例

xingyun86 2月前 109

C# HttpWebRequest访问网页通用封装代码实例

public HttpStatusCode HttpExec(out string respData, string reqUrl, WebHeaderCollection webHeaderCollection, string reqMethod = "GET", string postData = "", WebProxy webProxy = null)
{
    string contentEncoding = "";
    Stream responseStream = null;
    HttpWebRequest httpWebRequest = null;
    HttpWebResponse httpWebResponse = null;
    HttpStatusCode httpStatusCode = HttpStatusCode.Unused;
    try
    {
        respData = "";
        httpWebRequest = WebRequest.Create(reqUrl) as HttpWebRequest;
        if (httpWebRequest != null)
        {
            httpWebRequest.Proxy = webProxy;
            httpWebRequest.Method = reqMethod;
            webHeaderCollection["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0";
            webHeaderCollection["Accept-Language"] = "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6";
            if (webHeaderCollection != null)
            {
                foreach (var key in webHeaderCollection.AllKeys)
                {
                    httpWebRequest.Headers[key] = webHeaderCollection[key];
                }
            }
            if (postData.Length > 0)
            {
                httpWebRequest.ContentLength = postData.Length;
                using (Stream reqestStream = httpWebRequest.GetRequestStream())         //using(){}作为语句,用于定义一个范围,在此范围的末尾将释放对象。
                {
                    reqestStream.Write(Encoding.UTF8.GetBytes(postData), 0, postData.Length);
                }
            }
            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            if (httpWebResponse != null)
            {
                httpStatusCode = httpWebResponse.StatusCode;
                if (httpWebResponse.ContentEncoding != null)
                {
                    contentEncoding = httpWebResponse.ContentEncoding.ToUpperInvariant();
                }
                switch (contentEncoding)
                {
                    case ("GZIP"): responseStream = new GZipStream(httpWebResponse.GetResponseStream(), CompressionMode.Decompress); break;
                    case ("DEFLATE"): responseStream = new DeflateStream(httpWebResponse.GetResponseStream(), CompressionMode.Decompress); break;
                    default: responseStream = httpWebResponse.GetResponseStream(); break;
                }
                if (responseStream != null)
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        respData = streamReader.ReadToEnd();
                    }
                    responseStream.Dispose();
                }
            }
        }
    }
    catch (Exception e)
    {
        respData = e.Message;
    }
    return httpStatusCode;
}
public HttpStatusCode HttpOptions(out string respData, string reqUrl, WebHeaderCollection webHeaderCollection, WebProxy webProxy = null)
{
    return HttpExec(out respData, reqUrl, webHeaderCollection, "OPTIONS", "", webProxy);
}
public HttpStatusCode HttpGet(out string respData, string reqUrl, WebHeaderCollection webHeaderCollection, WebProxy webProxy = null)
{
    return HttpExec(out respData, reqUrl, webHeaderCollection, "GET", "", webProxy);
}
public HttpStatusCode HttpPost(out string respData, string reqUrl, WebHeaderCollection webHeaderCollection, string postData, WebProxy webProxy = null)
{
    return HttpExec(out respData, reqUrl, webHeaderCollection, "POST", postData, webProxy);
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回