一个特别不错的http请求类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;

namespace aaa
{
    public class ResponseModel
    {
        private WebHeaderCollection header;
        /// <summary>
        /// 返回的头部信息集合
        /// </summary>
        public WebHeaderCollection Header
        {
            get { return header; }
            set { header = value; }
        }
        private string html;
        /// <summary>
        /// 返回的文本内容
        /// </summary>
        public string Html
        {
            get { return html; }
            set { html = value; }
        }
        private Stream stream;
        /// <summary>
        /// 返回的流内容
        /// </summary>
        public Stream Stream
        {
            get { return stream; }
            set { stream = value; }
        }

    }
    public class HttpHelper
    {
        private string accept = "application/json,text/javascrip{过滤}t,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        private System.Net.CookieContainer cc = new System.Net.CookieContainer();
        private string contentType = "application/x-www-form-urlencoded";

        private int timeOut = 30000;
        public NameValueCollection Heads = new NameValueCollection();
        private bool AllowAutoRedirect = false;
        bool needReset = false;
        private System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");

        public IWebProxy Proxy;
        private string[] userAgents = new string[] { "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0" };

        private string userAgent
        {
            get
            {
                return this.userAgents[new Random().Next(0, this.userAgents.Length)];
            }
        }

        /// <summary>
        /// 设置下一次请求为自动重定向
        /// </summary>
        /// <param name="value"></param>
        public void SetAllowAutoRedirectOneTime(bool value)
        {
            AllowAutoRedirect = value;
            needReset = true;
        }

        /// <summary>
        /// 网页访问
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="isPost">是否Post</param>
        /// <param name="postData">Post数据内容</param>
        /// <param name="retType">返回类型0为文本,1为Stream</param>
        /// <param name="cookieContainer">cookie</param>
        /// <param name="refurl">Referer</param>
        /// <param name="_contentType">contentType</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public ResponseModel HttpVisit(string url, bool isPost = false, string postData = null, int retType = 0, System.Net.CookieContainer cookieContainer = null, string refurl = null, string _contentType = null, NameValueCollection headers = null)
        {
            if (cookieContainer == null)
            {
                cookieContainer = this.cc;
            }

            if (!isPost)
            {
                return GetHtml(url, refurl, cookieContainer, _contentType, headers, retType);
            }

            ResponseModel model = new ResponseModel();

            ServicePointManager.Expect100Continue = true;

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                byte[] bytes = System.Text.Encoding.Default.GetBytes(postData);
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.Timeout = timeOut;
                if (string.IsNullOrEmpty(_contentType))
                {
                    request.ContentType = this.contentType;
                }
                else
                {
                    request.ContentType = _contentType;
                }

                if (string.IsNullOrEmpty(refurl))
                {
                    request.Referer = url;
                }
                else
                {
                    request.Referer = refurl;
                }

                request.AllowAutoRedirect = AllowAutoRedirect;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;

                if (headers != null)
                {
                    request.Headers.Add(Heads);
                    request.Headers.Add(headers);
                }
                else
                {
                    request.Headers.Add(Heads);
                }

                request.Method = isPost ? "POST" : "GET";
                request.ContentLength = bytes.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                if (retType == 1)
                {
                    response = (HttpWebResponse)request.GetResponse();

                    model.Header = response.Headers;

                    Stream responseStream = response.GetResponseStream();

                    if (response.Cookies.Count > 0)
                    {
                        this.cc.Add(response.Cookies);
                    }

                    model.Stream = responseStream;
                    return model;

                }

                string str = string.Empty;
                response = (HttpWebResponse)request.GetResponse();

                model.Header = response.Headers;

                string encoding = "utf-8";

                if (!string.IsNullOrEmpty(response.CharacterSet))
                {
                    encoding = response.CharacterSet.ToLower();
                }
                else
                {
                    encoding = this.encoding.HeaderName;
                }

                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {

                    using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }
                    }
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }

                    }
                }
                else
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }
                    }
                }

                request.Abort();
                response.Close();
                request.Abort();
                if (response.Cookies.Count > 0)
                {
                    this.cc.Add(response.Cookies);
                }
                model.Html = str;
                return model;
            }
            catch (Exception ex)
            {
                if (request != null) request.Abort();
                if (response != null)
                {
                    response.Close();

                    return new ResponseModel() { Html = ex.Message, Header = response.Headers };
                }
                return new ResponseModel() { Html = ex.Message };
            }
            finally
            {
                if (needReset)
                {
                    AllowAutoRedirect = false;
                    needReset = false;
                }
            }
        }
        /// <summary>
        /// 清理string类型Cookie.剔除无用项返回结果为null时遇见错误.
        /// </summary>
        /// <param name="Cookies"></param>
        /// <returns></returns>
        public CookieCollection ClearCookie(string Cookies)
        {
            try
            {
                CookieCollection cookies = new CookieCollection();
                string rStr = string.Empty;
                Cookies = Cookies.Replace(";", "; ");
                Regex r = new Regex("(?<=,)(?<cookie>[^ ]+=(?!deleted;)[^;]+);");
                MatchCollection ms = r.Matches("," + Cookies);
                foreach (Match m in ms)
                {
                    string[] cookie = m.Groups["cookie"].Value.Split(‘=‘);

                    if (cookie.Length > 1)
                        cookies.Add(new Cookie(cookie[0], cookie[1]));

                }
                return cookies;
            }
            catch
            {
                return new CookieCollection();
            }
        }

        private ResponseModel GetHtml(string url, string refurl = null, System.Net.CookieContainer cookieContainer = null, string _contentType = "", NameValueCollection headers = null, int retType = 1)
        {
            if (cookieContainer == null)
            {
                cookieContainer = this.cc;
            }

            ResponseModel model = new ResponseModel();

            ServicePointManager.Expect100Continue = true;

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.Timeout = timeOut;
                if (string.IsNullOrEmpty(_contentType))
                {
                    request.ContentType = this.contentType;
                }
                else
                {
                    request.ContentType = _contentType;
                }

                if (string.IsNullOrEmpty(refurl))
                {
                    request.Referer = url;
                }
                else
                {
                    request.Referer = refurl;
                }

                request.AllowAutoRedirect = AllowAutoRedirect;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;

                if (headers != null)
                {
                    request.Headers.Add(Heads);
                    request.Headers.Add(headers);
                }
                else
                {
                    request.Headers.Add(Heads);
                }

                request.Method = "GET";

                if (retType == 1)
                {
                    response = (HttpWebResponse)request.GetResponse();

                    model.Header = response.Headers;

                    Stream responseStream = response.GetResponseStream();

                    if (response.Cookies.Count > 0)
                    {
                        this.cc.Add(response.Cookies);
                    }

                    model.Stream = responseStream;

                    return model;

                }

                string str = string.Empty;
                response = (HttpWebResponse)request.GetResponse();

                model.Header = response.Headers;

                string encoding = "utf-8";

                if (!string.IsNullOrEmpty(response.CharacterSet))
                {
                    encoding = response.CharacterSet.ToLower();
                }
                else
                {
                    encoding = this.encoding.HeaderName;
                }

                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {

                    using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }
                    }
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }

                    }
                }
                else
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
                        {

                            str = reader.ReadToEnd();
                        }
                    }
                }

                if (response.Cookies.Count > 0)
                {
                    cookieContainer.Add(response.Cookies);
                }

                request.Abort();
                response.Close();

                model.Html = str;
                return model;
            }
            catch (Exception ex)
            {
                if (request != null) request.Abort();
                if (response != null)
                {
                    response.Close();

                    return new ResponseModel() { Html = ex.Message, Header = response.Headers };
                }
                return new ResponseModel() { Html = ex.Message };
            }
            finally
            {
                if (needReset)
                {
                    AllowAutoRedirect = false;
                    needReset = false;
                }
            }
        }

        private bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            //直接通过HTTPS的证书请求
            return true;
        }

        public Stream GetStream(string url)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = this.cc;
                request.ContentType = this.contentType;
                //      request.ServicePoint.ConnectionLimit = this.maxTry;
                request.Timeout = 0x1388;
                request.Referer = url;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();
                // this.currentTry--;
                if (response.Cookies.Count > 0)
                {
                    this.cc.Add(response.Cookies);
                }
                return responseStream;
            }
            catch (Exception ex)
            {
                //   if (this.currentTry <= this.maxTry) this.GetHtml(url, cookieContainer);
                //   this.currentTry--;
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return null;
            }
        }

        #region String与CookieContainer互转
        /// <summary>
        /// 将String转CookieContainer
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public CookieContainer StringToCookie(string url, string cookie)
        {
            string[] arrCookie = cookie.Split(‘;‘);
            CookieContainer cookie_container = new CookieContainer();    //加载Cookie
            foreach (string sCookie in arrCookie)
            {
                if (sCookie.IndexOf("expires") > 0)
                    continue;
                cookie_container.SetCookies(new Uri(url), sCookie);
            }
            return cookie_container;
        }

        /// <summary>
        /// 将CookieContainer转换为string类型
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public string GetCookieString()
        {
            System.Collections.Generic.List<Cookie> lstCookies = new System.Collections.Generic.List<Cookie>();
            Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
                System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
            StringBuilder sb = new StringBuilder();
            foreach (object pathList in table.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
                    | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                    foreach (Cookie c in colCookies)
                    {
                        sb.Append(c.Name).Append("=").Append(c.Value).Append(";");
                    }
            }
            return sb.ToString();
        }
        #endregion

    }
}
时间: 2024-10-24 02:21:22

一个特别不错的http请求类的相关文章

一个特别不错的jQuery快捷键插件:js-hotkeys

这其实不是什么新技术,这个插件在很早前就已经发布了,之前有项目用到,所以分享出来添加方式的例子 jQuery.hotkeys.add('esc',function (){ //执行函数 }); jQuery.hotkeys.add('Ctrl+a',function (){ //执行函数 }); 支持的快捷键如下:(注意使用时避开浏览器默认的一些快捷键:如ctrl+s) PS:懒得引JS了,支持下面的这些快捷键,想用的小伙伴自己可以测试一下哦! 一个jQuery的热键(快捷键)的插件,可以让你很

block传值以及利用block封装一个网络请求类

1.block在俩个UIViewController间传值 最近刚学了几招block 的高级用法,其实就是利用block语法在俩个UIViewController之间传值,在这里分享给初学者,同时也方便我自己理解.我们知道UINavigationController类管理UIViewController的时候,利用的是"栈"的思想,在这里不做过多解释,切入正题,假设我们现在有俩个UIViewController,viewC1和viewC2,viewC1比viewC2先进入到UINavi

一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)

目录 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载) Http协议简述 HttpRequest类设计 请求部分 接收部分 关于上传和下载 Cpp实现 关于源码中的Logger 使用示例 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载) 最近写了点关于Http上传下载文件相关的,于是今天整理下代码. Http协议简述 HttpRequest类设计 使用示例 Http协议简述 协议:网络协议的简称,网络协议是通信计算机双方必须共同遵从

WorldWind源码剖析系列:下载请求类DownloadRequest

下载请求类DownloadRequest是各种下载请求的抽象基类,先派生出网络下载请求类WebDownloadRequest,再派生出地理空间下载请求类GeoSpatialDownloadRequest(抽象类),再派生出地形下载请求类TerrainDownloadRequest.这些类的类图如下. 抽象基类下载请求类DownloadRequest 抽象基类下载请求类DownloadRequest各个字段和属性的含义说明如下: internal static DownloadQueue Queu

ios中封装网络请求类

ios中封装网络请求类 #import "JSNetWork.h" //asiHttpRequest #import "ASIFormDataRequest.h" //xml 的解析 #import "UseXmlParser.h" //判断是否联网 #import "Reachability.h" //sbJson,判断json的解析 #import "JSON.h" @implementation JS

【Java&amp;Android开源库代码剖析】のandroid-async-http(如何设计一个优雅的Android网络请求框架,同时支持同步和异步请求)开篇

在<[Java&Android开源库代码剖析]のandroid-smart-image-view>一文中我们提到了android-async-http这个开源库,本文正式开篇来详细介绍这个库的实现,同时结合源码探讨如何设计一个优雅的Android网络请求框架.做过一段时间Android开发的同学应该对这个库不陌生,因为它对Apache的HttpClient API的封装使得开发者可以简洁优雅的实现网络请求和响应,并且同时支持同步和异步请求. 网络请求框架一般至少需要具备如下几个组件:1

基于Volley,Gson封装支持JWT无状态安全验证和数据防篡改的GsonRequest网络请求类

这段时间做新的Android项目的客户端和和REST API通讯框架架构设计,使用了很多新技术,最终的方案也相当简洁优雅,客户端只需要传Java对象,服务器端返回json字符串,自动解析成Java对象, 无状态安全验证基于JWT实现,JWT规范的细节可以参考我前面的文章.JWT的token和数据防篡改签名统一放在HTTP Header中,这样就实现了对请求内容和返回结果的无侵入性,服务器端也可以在全局过滤器中统一处理安全验证. Android客户端使用了Volley网络请求框架和Gson解析库,

发送一个简单的http get 请求并且响应

问题 如何发送一个简单的HTTP GET请求并且取回相应的HTTP响应. 设计 创建一个WebClient类的实例,然后使用它的DownloadData()方法. 方案 string uri = "http://server/path/WebForm.aspx"; WebClient wc = new WebClient(); Console.WriteLine("Sending an HTTP GET request to " + uri); byte[] bRe

Servlet(五):一个Servlet处理多个请求

一.为什么要使用一个Servlet来处理多个请求? 当浏览器发送了一次请求到服务器时,servlet容器会根据请求的url-pattern找到对应的Servlet类,执行对应的doPost或doGet方法,再将响应信息返回给浏览器,这种情况下,一个具体的Servlet类只能处理对应的web.xml中配置的url-pattern请求,一个Servlet类,一对配置信息.如果业务扩展,需要三个Servlet来处理请求,就需要再加上两个具体的Servlet类,两对配置信息,如果继续向上扩展,是不是会认