zoukankan      html  css  js  c++  java
  • HttpHelper工具类

    1. /// <summary>  
    2. /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理  
    3. /// 重要提示:请不要自行修改本类,如果因为你自己修改后将无法升级到新版本。如果确实有什么问题请到官方网站提建议,  
    4. /// 我们一定会及时修改  
    5. /// 编码日期:2011-09-20  
    6. /// 编 码 人:苏飞  
    7. /// 联系方式:361983679    
    8. /// 官方网址:http://www.sufeinet.com/thread-3-1-1.html  
    9. /// 修改日期:2015-03-25  
    10. /// 版 本 号:1.4.7  
    11. /// </summary>  
    12. using System;  
    13. using System.Collections.Generic;  
    14. using System.Text;  
    15. using System.Net;  
    16. using System.IO;  
    17. using System.Text.RegularExpressions;  
    18. using System.IO.Compression;  
    19. using System.Security.Cryptography.X509Certificates;  
    20. using System.Net.Security;  
    21.   
    22. namespace DotNet.Utilities  
    23. {  
    24.     /// <summary>  
    25.     /// Http连接操作帮助类  
    26.     /// </summary>  
    27.     public class HttpHelper  
    28.     {  
    29.  
    30.         #region 预定义方变量  
    31.         //默认的编码  
    32.         private Encoding encoding = Encoding.Default;  
    33.         //Post数据编码  
    34.         private Encoding postencoding = Encoding.Default;  
    35.         //HttpWebRequest对象用来发起请求  
    36.         private HttpWebRequest request = null;  
    37.         //获取影响流的数据对象  
    38.         private HttpWebResponse response = null;  
    39.         #endregion  
    40.  
    41.         #region Public  
    42.   
    43.         /// <summary>  
    44.         /// 根据相传入的数据,得到相应页面数据  
    45.         /// </summary>  
    46.         /// <param name="item">参数类对象</param>  
    47.         /// <returns>返回HttpResult类型</returns>  
    48.         public HttpResult GetHtml(HttpItem item)  
    49.         {  
    50.             //返回参数  
    51.             HttpResult result = new HttpResult();  
    52.             try  
    53.             {  
    54.                 //准备参数  
    55.                 SetRequest(item);  
    56.             }  
    57.             catch (Exception ex)  
    58.             {  
    59.                 result.Cookie = string.Empty;  
    60.                 result.Header = null;  
    61.                 result.Html = ex.Message;  
    62.                 result.StatusDescription = "配置参数时出错:" + ex.Message;  
    63.                 //配置参数时出错  
    64.                 return result;  
    65.             }  
    66.             try  
    67.             {  
    68.                 //请求数据  
    69.                 using (response = (HttpWebResponse)request.GetResponse())  
    70.                 {  
    71.                     GetData(item, result);  
    72.                 }  
    73.             }  
    74.             catch (WebException ex)  
    75.             {  
    76.                 if (ex.Response != null)  
    77.                 {  
    78.                     using (response = (HttpWebResponse)ex.Response)  
    79.                     {  
    80.                         GetData(item, result);  
    81.                     }  
    82.                 }  
    83.                 else  
    84.                 {  
    85.                     result.Html = ex.Message;  
    86.                 }  
    87.             }  
    88.             catch (Exception ex)  
    89.             {  
    90.                 result.Html = ex.Message;  
    91.             }  
    92.             if (item.IsToLower) result.Html = result.Html.ToLower();  
    93.             return result;  
    94.         }  
    95.         #endregion  
    96.  
    97.         #region GetData  
    98.   
    99.         /// <summary>  
    100.         /// 获取数据的并解析的方法  
    101.         /// </summary>  
    102.         /// <param name="item"></param>  
    103.         /// <param name="result"></param>  
    104.         private void GetData(HttpItem item, HttpResult result)  
    105.         {  
    106.             #region base  
    107.             //获取StatusCode  
    108.             result.StatusCode = response.StatusCode;  
    109.             //获取StatusDescription  
    110.             result.StatusDescription = response.StatusDescription;  
    111.             //获取Headers  
    112.             result.Header = response.Headers;  
    113.             //获取CookieCollection  
    114.             if (response.Cookies != null) result.CookieCollection = response.Cookies;  
    115.             //获取set-cookie  
    116.             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];  
    117.             #endregion  
    118.  
    119.             #region byte  
    120.             //处理网页Byte  
    121.             byte[] ResponseByte = GetByte();  
    122.             #endregion  
    123.  
    124.             #region Html  
    125.             if (ResponseByte != null & ResponseByte.Length > 0)  
    126.             {  
    127.                 //设置编码  
    128.                 SetEncoding(item, result, ResponseByte);  
    129.                 //得到返回的HTML  
    130.                 result.Html = encoding.GetString(ResponseByte);  
    131.             }  
    132.             else  
    133.             {  
    134.                 //没有返回任何Html代码  
    135.                 result.Html = string.Empty;  
    136.             }  
    137.             #endregion  
    138.         }  
    139.         /// <summary>  
    140.         /// 设置编码  
    141.         /// </summary>  
    142.         /// <param name="item">HttpItem</param>  
    143.         /// <param name="result">HttpResult</param>  
    144.         /// <param name="ResponseByte">byte[]</param>  
    145.         private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)  
    146.         {  
    147.             //是否返回Byte类型数据  
    148.             if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;  
    149.             //从这里开始我们要无视编码了  
    150.             if (encoding == null)  
    151.             {  
    152.                 Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<]*charset=([^<]*)["']", RegexOptions.IgnoreCase);  
    153.                 string c = string.Empty;  
    154.                 if (meta != null && meta.Groups.Count > 0)  
    155.                 {  
    156.                     c = meta.Groups[1].Value.ToLower().Trim();  
    157.                 }  
    158.                 if (c.Length > 2)  
    159.                 {  
    160.                     try  
    161.                     {  
    162.                         encoding = Encoding.GetEncoding(c.Replace(""", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());  
    163.                     }  
    164.                     catch  
    165.                     {  
    166.                         if (string.IsNullOrEmpty(response.CharacterSet))  
    167.                         {  
    168.                             encoding = Encoding.UTF8;  
    169.                         }  
    170.                         else  
    171.                         {  
    172.                             encoding = Encoding.GetEncoding(response.CharacterSet);  
    173.                         }  
    174.                     }  
    175.                 }  
    176.                 else  
    177.                 {  
    178.                     if (string.IsNullOrEmpty(response.CharacterSet))  
    179.                     {  
    180.                         encoding = Encoding.UTF8;  
    181.                     }  
    182.                     else  
    183.                     {  
    184.                         encoding = Encoding.GetEncoding(response.CharacterSet);  
    185.                     }  
    186.                 }  
    187.             }  
    188.         }  
    189.         /// <summary>  
    190.         /// 提取网页Byte  
    191.         /// </summary>  
    192.         /// <returns></returns>  
    193.         private byte[] GetByte()  
    194.         {  
    195.             byte[] ResponseByte = null;  
    196.             MemoryStream _stream = new MemoryStream();  
    197.   
    198.             //GZIIP处理  
    199.             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))  
    200.             {  
    201.                 //开始读取流并设置编码方式  
    202.                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));  
    203.             }  
    204.             else  
    205.             {  
    206.                 //开始读取流并设置编码方式  
    207.                 _stream = GetMemoryStream(response.GetResponseStream());  
    208.             }  
    209.             //获取Byte  
    210.             ResponseByte = _stream.ToArray();  
    211.             _stream.Close();  
    212.             return ResponseByte;  
    213.         }  
    214.   
    215.         /// <summary>  
    216.         /// 4.0以下.net版本取数据使用  
    217.         /// </summary>  
    218.         /// <param name="streamResponse">流</param>  
    219.         private MemoryStream GetMemoryStream(Stream streamResponse)  
    220.         {  
    221.             MemoryStream _stream = new MemoryStream();  
    222.             int Length = 256;  
    223.             Byte[] buffer = new Byte[Length];  
    224.             int bytesRead = streamResponse.Read(buffer, 0, Length);  
    225.             while (bytesRead > 0)  
    226.             {  
    227.                 _stream.Write(buffer, 0, bytesRead);  
    228.                 bytesRead = streamResponse.Read(buffer, 0, Length);  
    229.             }  
    230.             return _stream;  
    231.         }  
    232.         #endregion  
    233.  
    234.         #region SetRequest  
    235.   
    236.         /// <summary>  
    237.         /// 为请求准备参数  
    238.         /// </summary>  
    239.         ///<param name="item">参数列表</param>  
    240.         private void SetRequest(HttpItem item)  
    241.         {  
    242.             // 验证证书  
    243.             SetCer(item);  
    244.             //设置Header参数  
    245.             if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)  
    246.                 {  
    247.                     request.Headers.Add(key, item.Header[key]);  
    248.                 }  
    249.             // 设置代理  
    250.             SetProxy(item);  
    251.             if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;  
    252.             request.ServicePoint.Expect100Continue = item.Expect100Continue;  
    253.             //请求方式Get或者Post  
    254.             request.Method = item.Method;  
    255.             request.Timeout = item.Timeout;  
    256.             request.KeepAlive = item.KeepAlive;  
    257.             request.ReadWriteTimeout = item.ReadWriteTimeout;  
    258.             if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);  
    259.             //Accept  
    260.             request.Accept = item.Accept;  
    261.             //ContentType返回类型  
    262.             request.ContentType = item.ContentType;  
    263.             //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息  
    264.             request.UserAgent = item.UserAgent;  
    265.             // 编码  
    266.             encoding = item.Encoding;  
    267.             //设置安全凭证  
    268.             request.Credentials = item.ICredentials;  
    269.             //设置Cookie  
    270.             SetCookie(item);  
    271.             //来源地址  
    272.             request.Referer = item.Referer;  
    273.             //是否执行跳转功能  
    274.             request.AllowAutoRedirect = item.Allowautoredirect;  
    275.             if (item.MaximumAutomaticRedirections > 0)  
    276.             {  
    277.                 request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;  
    278.             }  
    279.             //设置Post数据  
    280.             SetPostData(item);  
    281.             //设置最大连接  
    282.             if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;  
    283.         }  
    284.         /// <summary>  
    285.         /// 设置证书  
    286.         /// </summary>  
    287.         /// <param name="item"></param>  
    288.         private void SetCer(HttpItem item)  
    289.         {  
    290.             if (!string.IsNullOrEmpty(item.CerPath))  
    291.             {  
    292.                 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。  
    293.                 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);  
    294.                 //初始化对像,并设置请求的URL地址  
    295.                 request = (HttpWebRequest)WebRequest.Create(item.URL);  
    296.                 SetCerList(item);  
    297.                 //将证书添加到请求里  
    298.                 request.ClientCertificates.Add(new X509Certificate(item.CerPath));  
    299.             }  
    300.             else  
    301.             {  
    302.                 //初始化对像,并设置请求的URL地址  
    303.                 request = (HttpWebRequest)WebRequest.Create(item.URL);  
    304.                 SetCerList(item);  
    305.             }  
    306.         }  
    307.         /// <summary>  
    308.         /// 设置多个证书  
    309.         /// </summary>  
    310.         /// <param name="item"></param>  
    311.         private void SetCerList(HttpItem item)  
    312.         {  
    313.             if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)  
    314.             {  
    315.                 foreach (X509Certificate c in item.ClentCertificates)  
    316.                 {  
    317.                     request.ClientCertificates.Add(c);  
    318.                 }  
    319.             }  
    320.         }  
    321.         /// <summary>  
    322.         /// 设置Cookie  
    323.         /// </summary>  
    324.         /// <param name="item">Http参数</param>  
    325.         private void SetCookie(HttpItem item)  
    326.         {  
    327.             if (!string.IsNullOrEmpty(item.Cookie)) request.Headers[HttpRequestHeader.Cookie] = item.Cookie;  
    328.             //设置CookieCollection  
    329.             if (item.ResultCookieType == ResultCookieType.CookieCollection)  
    330.             {  
    331.                 request.CookieContainer = new CookieContainer();  
    332.                 if (item.CookieCollection != null && item.CookieCollection.Count > 0)  
    333.                     request.CookieContainer.Add(item.CookieCollection);  
    334.             }  
    335.         }  
    336.         /// <summary>  
    337.         /// 设置Post数据  
    338.         /// </summary>  
    339.         /// <param name="item">Http参数</param>  
    340.         private void SetPostData(HttpItem item)  
    341.         {  
    342.             //验证在得到结果时是否有传入数据  
    343.             if (!request.Method.Trim().ToLower().Contains("get"))  
    344.             {  
    345.                 if (item.PostEncoding != null)  
    346.                 {  
    347.                     postencoding = item.PostEncoding;  
    348.                 }  
    349.                 byte[] buffer = null;  
    350.                 //写入Byte类型  
    351.                 if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)  
    352.                 {  
    353.                     //验证在得到结果时是否有传入数据  
    354.                     buffer = item.PostdataByte;  
    355.                 }//写入文件  
    356.                 else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))  
    357.                 {  
    358.                     StreamReader r = new StreamReader(item.Postdata, postencoding);  
    359.                     buffer = postencoding.GetBytes(r.ReadToEnd());  
    360.                     r.Close();  
    361.                 } //写入字符串  
    362.                 else if (!string.IsNullOrEmpty(item.Postdata))  
    363.                 {  
    364.                     buffer = postencoding.GetBytes(item.Postdata);  
    365.                 }  
    366.                 if (buffer != null)  
    367.                 {  
    368.                     request.ContentLength = buffer.Length;  
    369.                     request.GetRequestStream().Write(buffer, 0, buffer.Length);  
    370.                 }  
    371.             }  
    372.         }  
    373.         /// <summary>  
    374.         /// 设置代理  
    375.         /// </summary>  
    376.         /// <param name="item">参数对象</param>  
    377.         private void SetProxy(HttpItem item)  
    378.         {  
    379.             bool isIeProxy = false;  
    380.             if (!string.IsNullOrEmpty(item.ProxyIp))  
    381.             {  
    382.                 isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy");  
    383.             }  
    384.             if (!string.IsNullOrEmpty(item.ProxyIp) && !isIeProxy)  
    385.             {  
    386.                 //设置代理服务器  
    387.                 if (item.ProxyIp.Contains(":"))  
    388.                 {  
    389.                     string[] plist = item.ProxyIp.Split(':');  
    390.                     WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));  
    391.                     //建议连接  
    392.                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);  
    393.                     //给当前请求对象  
    394.                     request.Proxy = myProxy;  
    395.                 }  
    396.                 else  
    397.                 {  
    398.                     WebProxy myProxy = new WebProxy(item.ProxyIp, false);  
    399.                     //建议连接  
    400.                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);  
    401.                     //给当前请求对象  
    402.                     request.Proxy = myProxy;  
    403.                 }  
    404.             }  
    405.             else if (isIeProxy)  
    406.             {  
    407.                 //设置为IE代理  
    408.             }  
    409.             else  
    410.             {  
    411.                 request.Proxy = item.WebProxy;  
    412.             }  
    413.         }  
    414.         #endregion  
    415.  
    416.         #region private main  
    417.         /// <summary>  
    418.         /// 回调验证证书问题  
    419.         /// </summary>  
    420.         /// <param name="sender">流对象</param>  
    421.         /// <param name="certificate">证书</param>  
    422.         /// <param name="chain">X509Chain</param>  
    423.         /// <param name="errors">SslPolicyErrors</param>  
    424.         /// <returns>bool</returns>  
    425.         private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }  
    426.         #endregion  
    427.     }  
    428.     /// <summary>  
    429.     /// Http请求参考类  
    430.     /// </summary>  
    431.     public class HttpItem  
    432.     {  
    433.         string _URL = string.Empty;  
    434.         /// <summary>  
    435.         /// 请求URL必须填写  
    436.         /// </summary>  
    437.         public string URL  
    438.         {  
    439.             get { return _URL; }  
    440.             set { _URL = value; }  
    441.         }  
    442.         string _Method = "GET";  
    443.         /// <summary>  
    444.         /// 请求方式默认为GET方式,当为POST方式时必须设置Postdata的值  
    445.         /// </summary>  
    446.         public string Method  
    447.         {  
    448.             get { return _Method; }  
    449.             set { _Method = value; }  
    450.         }  
    451.         int _Timeout = 100000;  
    452.         /// <summary>  
    453.         /// 默认请求超时时间  
    454.         /// </summary>  
    455.         public int Timeout  
    456.         {  
    457.             get { return _Timeout; }  
    458.             set { _Timeout = value; }  
    459.         }  
    460.         int _ReadWriteTimeout = 30000;  
    461.         /// <summary>  
    462.         /// 默认写入Post数据超时间  
    463.         /// </summary>  
    464.         public int ReadWriteTimeout  
    465.         {  
    466.             get { return _ReadWriteTimeout; }  
    467.             set { _ReadWriteTimeout = value; }  
    468.         }  
    469.         Boolean _KeepAlive = true;  
    470.         /// <summary>  
    471.         ///  获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接默认为true。  
    472.         /// </summary>  
    473.         public Boolean KeepAlive  
    474.         {  
    475.             get { return _KeepAlive; }  
    476.             set { _KeepAlive = value; }  
    477.         }  
    478.         string _Accept = "text/html, application/xhtml+xml, */*";  
    479.         /// <summary>  
    480.         /// 请求标头值 默认为text/html, application/xhtml+xml, */*  
    481.         /// </summary>  
    482.         public string Accept  
    483.         {  
    484.             get { return _Accept; }  
    485.             set { _Accept = value; }  
    486.         }  
    487.         string _ContentType = "text/html";  
    488.         /// <summary>  
    489.         /// 请求返回类型默认 text/html  
    490.         /// </summary>  
    491.         public string ContentType  
    492.         {  
    493.             get { return _ContentType; }  
    494.             set { _ContentType = value; }  
    495.         }  
    496.         string _UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";  
    497.         /// <summary>  
    498.         /// 客户端访问信息默认Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)  
    499.         /// </summary>  
    500.         public string UserAgent  
    501.         {  
    502.             get { return _UserAgent; }  
    503.             set { _UserAgent = value; }  
    504.         }  
    505.         Encoding _Encoding = null;  
    506.         /// <summary>  
    507.         /// 返回数据编码默认为NUll,可以自动识别,一般为utf-8,gbk,gb2312  
    508.         /// </summary>  
    509.         public Encoding Encoding  
    510.         {  
    511.             get { return _Encoding; }  
    512.             set { _Encoding = value; }  
    513.         }  
    514.         private PostDataType _PostDataType = PostDataType.String;  
    515.         /// <summary>  
    516.         /// Post的数据类型  
    517.         /// </summary>  
    518.         public PostDataType PostDataType  
    519.         {  
    520.             get { return _PostDataType; }  
    521.             set { _PostDataType = value; }  
    522.         }  
    523.         string _Postdata = string.Empty;  
    524.         /// <summary>  
    525.         /// Post请求时要发送的字符串Post数据  
    526.         /// </summary>  
    527.         public string Postdata  
    528.         {  
    529.             get { return _Postdata; }  
    530.             set { _Postdata = value; }  
    531.         }  
    532.         private byte[] _PostdataByte = null;  
    533.         /// <summary>  
    534.         /// Post请求时要发送的Byte类型的Post数据  
    535.         /// </summary>  
    536.         public byte[] PostdataByte  
    537.         {  
    538.             get { return _PostdataByte; }  
    539.             set { _PostdataByte = value; }  
    540.         }  
    541.         private WebProxy _WebProxy;  
    542.         /// <summary>  
    543.         /// 设置代理对象,不想使用IE默认配置就设置为Null,而且不要设置ProxyIp  
    544.         /// </summary>  
    545.         public WebProxy WebProxy  
    546.         {  
    547.             get { return _WebProxy; }  
    548.             set { _WebProxy = value; }  
    549.         }  
    550.   
    551.         CookieCollection cookiecollection = null;  
    552.         /// <summary>  
    553.         /// Cookie对象集合  
    554.         /// </summary>  
    555.         public CookieCollection CookieCollection  
    556.         {  
    557.             get { return cookiecollection; }  
    558.             set { cookiecollection = value; }  
    559.         }  
    560.         string _Cookie = string.Empty;  
    561.         /// <summary>  
    562.         /// 请求时的Cookie  
    563.         /// </summary>  
    564.         public string Cookie  
    565.         {  
    566.             get { return _Cookie; }  
    567.             set { _Cookie = value; }  
    568.         }  
    569.         string _Referer = string.Empty;  
    570.         /// <summary>  
    571.         /// 来源地址,上次访问地址  
    572.         /// </summary>  
    573.         public string Referer  
    574.         {  
    575.             get { return _Referer; }  
    576.             set { _Referer = value; }  
    577.         }  
    578.         string _CerPath = string.Empty;  
    579.         /// <summary>  
    580.         /// 证书绝对路径  
    581.         /// </summary>  
    582.         public string CerPath  
    583.         {  
    584.             get { return _CerPath; }  
    585.             set { _CerPath = value; }  
    586.         }  
    587.         private Boolean isToLower = false;  
    588.         /// <summary>  
    589.         /// 是否设置为全文小写,默认为不转化  
    590.         /// </summary>  
    591.         public Boolean IsToLower  
    592.         {  
    593.             get { return isToLower; }  
    594.             set { isToLower = value; }  
    595.         }  
    596.         private Boolean allowautoredirect = false;  
    597.         /// <summary>  
    598.         /// 支持跳转页面,查询结果将是跳转后的页面,默认是不跳转  
    599.         /// </summary>  
    600.         public Boolean Allowautoredirect  
    601.         {  
    602.             get { return allowautoredirect; }  
    603.             set { allowautoredirect = value; }  
    604.         }  
    605.         private int connectionlimit = 1024;  
    606.         /// <summary>  
    607.         /// 最大连接数  
    608.         /// </summary>  
    609.         public int Connectionlimit  
    610.         {  
    611.             get { return connectionlimit; }  
    612.             set { connectionlimit = value; }  
    613.         }  
    614.         private string proxyusername = string.Empty;  
    615.         /// <summary>  
    616.         /// 代理Proxy 服务器用户名  
    617.         /// </summary>  
    618.         public string ProxyUserName  
    619.         {  
    620.             get { return proxyusername; }  
    621.             set { proxyusername = value; }  
    622.         }  
    623.         private string proxypwd = string.Empty;  
    624.         /// <summary>  
    625.         /// 代理 服务器密码  
    626.         /// </summary>  
    627.         public string ProxyPwd  
    628.         {  
    629.             get { return proxypwd; }  
    630.             set { proxypwd = value; }  
    631.         }  
    632.         private string proxyip = string.Empty;  
    633.         /// <summary>  
    634.         /// 代理 服务IP ,如果要使用IE代理就设置为ieproxy  
    635.         /// </summary>  
    636.         public string ProxyIp  
    637.         {  
    638.             get { return proxyip; }  
    639.             set { proxyip = value; }  
    640.         }  
    641.         private ResultType resulttype = ResultType.String;  
    642.         /// <summary>  
    643.         /// 设置返回类型String和Byte  
    644.         /// </summary>  
    645.         public ResultType ResultType  
    646.         {  
    647.             get { return resulttype; }  
    648.             set { resulttype = value; }  
    649.         }  
    650.         private WebHeaderCollection header = new WebHeaderCollection();  
    651.         /// <summary>  
    652.         /// header对象  
    653.         /// </summary>  
    654.         public WebHeaderCollection Header  
    655.         {  
    656.             get { return header; }  
    657.             set { header = value; }  
    658.         }  
    659.   
    660.         private Version _ProtocolVersion;  
    661.   
    662.         /// <summary>  
    663.         //     获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。  
    664.         /// </summary>  
    665.         public Version ProtocolVersion  
    666.         {  
    667.             get { return _ProtocolVersion; }  
    668.             set { _ProtocolVersion = value; }  
    669.         }  
    670.         private Boolean _expect100continue = true;  
    671.         /// <summary>  
    672.         ///  获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。  
    673.         /// </summary>  
    674.         public Boolean Expect100Continue  
    675.         {  
    676.             get { return _expect100continue; }  
    677.             set { _expect100continue = value; }  
    678.         }  
    679.         private X509CertificateCollection _ClentCertificates;  
    680.         /// <summary>  
    681.         /// 设置509证书集合  
    682.         /// </summary>  
    683.         public X509CertificateCollection ClentCertificates  
    684.         {  
    685.             get { return _ClentCertificates; }  
    686.             set { _ClentCertificates = value; }  
    687.         }  
    688.         private Encoding _PostEncoding;  
    689.         /// <summary>  
    690.         /// 设置或获取Post参数编码,默认的为Default编码  
    691.         /// </summary>  
    692.         public Encoding PostEncoding  
    693.         {  
    694.             get { return _PostEncoding; }  
    695.             set { _PostEncoding = value; }  
    696.         }  
    697.         private ResultCookieType _ResultCookieType = ResultCookieType.String;  
    698.         /// <summary>  
    699.         /// Cookie返回类型,默认的是只返回字符串类型  
    700.         /// </summary>  
    701.         public ResultCookieType ResultCookieType  
    702.         {  
    703.             get { return _ResultCookieType; }  
    704.             set { _ResultCookieType = value; }  
    705.         }  
    706.   
    707.         private ICredentials _ICredentials = CredentialCache.DefaultCredentials;  
    708.         /// <summary>  
    709.         /// 获取或设置请求的身份验证信息。  
    710.         /// </summary>  
    711.         public ICredentials ICredentials  
    712.         {  
    713.             get { return _ICredentials; }  
    714.             set { _ICredentials = value; }  
    715.         }  
    716.         /// <summary>  
    717.         /// 设置请求将跟随的重定向的最大数目  
    718.         /// </summary>  
    719.         private int _MaximumAutomaticRedirections;  
    720.   
    721.         public int MaximumAutomaticRedirections  
    722.         {  
    723.             get { return _MaximumAutomaticRedirections; }  
    724.             set { _MaximumAutomaticRedirections = value; }  
    725.         }  
    726.   
    727.         private DateTime? _IfModifiedSince = null;  
    728.         /// <summary>  
    729.         /// 获取和设置IfModifiedSince,默认为当前日期和时间  
    730.         /// </summary>  
    731.         public DateTime? IfModifiedSince  
    732.         {  
    733.             get { return _IfModifiedSince; }  
    734.             set { _IfModifiedSince = value; }  
    735.         }  
    736.   
    737.     }  
    738.     /// <summary>  
    739.     /// Http返回参数类  
    740.     /// </summary>  
    741.     public class HttpResult  
    742.     {  
    743.         private string _Cookie;  
    744.         /// <summary>  
    745.         /// Http请求返回的Cookie  
    746.         /// </summary>  
    747.         public string Cookie  
    748.         {  
    749.             get { return _Cookie; }  
    750.             set { _Cookie = value; }  
    751.         }  
    752.   
    753.         private CookieCollection _CookieCollection;  
    754.         /// <summary>  
    755.         /// Cookie对象集合  
    756.         /// </summary>  
    757.         public CookieCollection CookieCollection  
    758.         {  
    759.             get { return _CookieCollection; }  
    760.             set { _CookieCollection = value; }  
    761.         }  
    762.         private string _html = string.Empty;  
    763.         /// <summary>  
    764.         /// 返回的String类型数据 只有ResultType.String时才返回数据,其它情况为空  
    765.         /// </summary>  
    766.         public string Html  
    767.         {  
    768.             get { return _html; }  
    769.             set { _html = value; }  
    770.         }  
    771.         private byte[] _ResultByte;  
    772.         /// <summary>  
    773.         /// 返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空  
    774.         /// </summary>  
    775.         public byte[] ResultByte  
    776.         {  
    777.             get { return _ResultByte; }  
    778.             set { _ResultByte = value; }  
    779.         }  
    780.         private WebHeaderCollection _Header;  
    781.         /// <summary>  
    782.         /// header对象  
    783.         /// </summary>  
    784.         public WebHeaderCollection Header  
    785.         {  
    786.             get { return _Header; }  
    787.             set { _Header = value; }  
    788.         }  
    789.         private string _StatusDescription;  
    790.         /// <summary>  
    791.         /// 返回状态说明  
    792.         /// </summary>  
    793.         public string StatusDescription  
    794.         {  
    795.             get { return _StatusDescription; }  
    796.             set { _StatusDescription = value; }  
    797.         }  
    798.         private HttpStatusCode _StatusCode;  
    799.         /// <summary>  
    800.         /// 返回状态码,默认为OK  
    801.         /// </summary>  
    802.         public HttpStatusCode StatusCode  
    803.         {  
    804.             get { return _StatusCode; }  
    805.             set { _StatusCode = value; }  
    806.         }  
    807.     }  
    808.     /// <summary>  
    809.     /// 返回类型  
    810.     /// </summary>  
    811.     public enum ResultType  
    812.     {  
    813.         /// <summary>  
    814.         /// 表示只返回字符串 只有Html有数据  
    815.         /// </summary>  
    816.         String,  
    817.         /// <summary>  
    818.         /// 表示返回字符串和字节流 ResultByte和Html都有数据返回  
    819.         /// </summary>  
    820.         Byte  
    821.     }  
    822.     /// <summary>  
    823.     /// Post的数据格式默认为string  
    824.     /// </summary>  
    825.     public enum PostDataType  
    826.     {  
    827.         /// <summary>  
    828.         /// 字符串类型,这时编码Encoding可不设置  
    829.         /// </summary>  
    830.         String,  
    831.         /// <summary>  
    832.         /// Byte类型,需要设置PostdataByte参数的值编码Encoding可设置为空  
    833.         /// </summary>  
    834.         Byte,  
    835.         /// <summary>  
    836.         /// 传文件,Postdata必须设置为文件的绝对路径,必须设置Encoding的值  
    837.         /// </summary>  
    838.         FilePath  
    839.     }  
    840.     /// <summary>  
    841.     /// Cookie返回类型  
    842.     /// </summary>  
    843.     public enum ResultCookieType  
    844.     {  
    845.         /// <summary>  
    846.         /// 只返回字符串类型的Cookie  
    847.         /// </summary>  
    848.         String,  
    849.         /// <summary>  
    850.         /// CookieCollection格式的Cookie集合同时也返回String类型的cookie  
    851.         /// </summary>  
    852.         CookieCollection  
    853.     }  
    854. }  
  • 相关阅读:
    2019年2月8日训练日记(文件操作知识点小结)
    2019年2月7日训练日记
    2019年2月6日训练日记
    2019年2月5日训练日记
    2019年2月4日训练日记(递归学习小结)
    【Java】Java中的IO流
    【Java】Java中线程的使用
    【Java】Java图形化用户界面-GUI
    【Java】Java中的集合类
    C++程序学习之实现手机通讯录功能模拟
  • 原文地址:https://www.cnblogs.com/dangzhenjiuhao/p/5622038.html
Copyright © 2011-2022 走看看