挂海论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
 友情提示:文字/图片广告均非网站意见,请担保交易勿直接付款,由此产生的责任自负
游戏交易就上寄售网-专注稳定-诚信赢天下玩游戏来117游戏网(H5不下载也能玩手游传奇,吃鸡,竞技都有)天下盾/国内/免实名/免备案CDN无视一切CC/DD攻击 找塔科夫作者TG @wuhao1954 QQ283931494 →入驻S9企业发卡网各种全黑号辅助群:475351077
██【我要租此广告位】██... .
查看: 3349|回复: 3
打印 上一主题 下一主题

HttpHelper类源码,方便网络抓取

[复制链接]

9

积分

1

主题

0

听众
已帮网友解决0 个问题
好评
0
贡献
8
海币
9
交易币
0
跳转到指定楼层
楼主
发表于 2016-3-30 14:56:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提醒:若下载的软件是收费的"请不要付款",可能是骗子,请立即联系本站举报,执意要付款被骗后本站概不负责。(任何交易请走第三方中介,请勿直接付款交易以免被骗!切记).

友情提示:文字/图片广告均非本站意见,请担保交易勿直接付款,由此产生的责任自负!!!↑↑


  1. public class HttpHelper
  2.     {

  3.         #region 预定义方变量
  4.         //默认的编码
  5.         private Encoding encoding = Encoding.Default;
  6.         //Post数据编码
  7.         private Encoding postencoding = Encoding.Default;
  8.         //HttpWebRequest对象用来发起请求
  9.         private HttpWebRequest request = null;
  10.         //获取影响流的数据对象
  11.         private HttpWebResponse response = null;
  12.         #endregion

  13.         #region Public

  14.         /// <summary>
  15.         /// 根据相传入的数据,得到相应页面数据
  16.         /// </summary>
  17.         /// <param name="item">参数类对象</param>
  18.         /// <returns>返回HttpResult类型</returns>
  19.         public HttpResult GetHtml(HttpItem item)
  20.         {
  21.             //返回参数
  22.             HttpResult result = new HttpResult();
  23.             try
  24.             {
  25.                 //准备参数
  26.                 SetRequest(item);
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 result.Cookie = string.Empty;
  31.                 result.Header = null;
  32.                 result.Html = ex.Message;
  33.                 result.StatusDescription = "配置参数时出错:" + ex.Message;
  34.                 //配置参数时出错
  35.                 return result;
  36.             }
  37.             try
  38.             {
  39.                 //request.KeepAlive = true;
  40.                 //请求数据
  41.                 using(response = (HttpWebResponse)request.GetResponse())
  42.                
  43.                     GetData(item, result);
  44.                
  45.             }
  46.             catch (WebException ex)
  47.             {
  48.                 if (ex.Response != null)
  49.                 {
  50.                     using(response = (HttpWebResponse)ex.Response)
  51.                     GetData(item, result);
  52.                     
  53.                 }
  54.                 else
  55.                 {
  56.                     result.Html = ex.Message;
  57.                 }
  58.             }
  59.             catch (Exception ex)
  60.             {
  61.                 result.Html = ex.Message;
  62.             }
  63.             if (item.IsToLower) result.Html = result.Html.ToLower();
  64.             return result;
  65.         }
  66.         #endregion

  67.         #region GetData

  68.         /// <summary>
  69.         /// 获取数据的并解析的方法
  70.         /// </summary>
  71.         /// <param name="item"></param>
  72.         /// <param name="result"></param>
  73.         private void GetData(HttpItem item, HttpResult result)
  74.         {
  75.             #region base
  76.             //获取StatusCode
  77.             result.StatusCode = response.StatusCode;
  78.             //获取StatusDescription
  79.             result.StatusDescription = response.StatusDescription;
  80.             //获取Headers
  81.             result.Header = response.Headers;
  82.             //获取CookieCollection
  83.             if (response.Cookies != null) result.CookieCollection = response.Cookies;
  84.             //获取set-cookie
  85.             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
  86.             #endregion

  87.             #region byte
  88.             //处理网页Byte
  89.             byte[] ResponseByte = GetByte();
  90.             #endregion

  91.             #region Html
  92.             if (ResponseByte != null & ResponseByte.Length > 0)
  93.             {
  94.                 //设置编码
  95.                 SetEncoding(item, result, ResponseByte);
  96.                 //得到返回的HTML
  97.                 result.Html = encoding.GetString(ResponseByte);
  98.             }
  99.             else
  100.             {
  101.                 //没有返回任何Html代码
  102.                 result.Html = string.Empty;
  103.             }
  104.             #endregion
  105.         }
  106.         /// <summary>
  107.         /// 设置编码
  108.         /// </summary>
  109.         /// <param name="item">HttpItem</param>
  110.         /// <param name="result">HttpResult</param>
  111.         /// <param name="ResponseByte">byte[]</param>
  112.         private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)
  113.         {
  114.             //是否返回Byte类型数据
  115.             if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
  116.             //从这里开始我们要无视编码了
  117.             if (encoding == null)
  118.             {
  119.                 Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
  120.                 string c = string.Empty;
  121.                 if (meta != null && meta.Groups.Count > 0)
  122.                 {
  123.                     c = meta.Groups[1].Value.ToLower().Trim();
  124.                 }
  125.                 if (c.Length > 2)
  126.                 {
  127.                     try
  128.                     {
  129.                         encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
  130.                     }
  131.                     catch
  132.                     {
  133.                         if (string.IsNullOrEmpty(response.CharacterSet))
  134.                         {
  135.                             encoding = Encoding.UTF8;
  136.                         }
  137.                         else
  138.                         {
  139.                             encoding = Encoding.GetEncoding(response.CharacterSet);
  140.                         }
  141.                     }
  142.                 }
  143.                 else
  144.                 {
  145.                     if (string.IsNullOrEmpty(response.CharacterSet))
  146.                     {
  147.                         encoding = Encoding.UTF8;
  148.                     }
  149.                     else
  150.                     {
  151.                         encoding = Encoding.GetEncoding(response.CharacterSet);
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.         /// <summary>
  157.         /// 提取网页Byte
  158.         /// </summary>
  159.         /// <returns></returns>
  160.         private byte[] GetByte()
  161.         {
  162.             byte[] ResponseByte = null;
  163.             MemoryStream _stream = new MemoryStream();

  164.             //GZIIP处理
  165.             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
  166.             {
  167.                 //开始读取流并设置编码方式
  168.                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
  169.             }
  170.             else
  171.             {
  172.                 //开始读取流并设置编码方式
  173.                 _stream = GetMemoryStream(response.GetResponseStream());
  174.             }
  175.             //获取Byte
  176.             ResponseByte = _stream.ToArray();
  177.             _stream.Close();
  178.             return ResponseByte;
  179.         }

  180.         /// <summary>
  181.         /// 4.0以下.net版本取数据使用
  182.         /// </summary>
  183.         /// <param name="streamResponse">流</param>
  184.         private MemoryStream GetMemoryStream(Stream streamResponse)
  185.         {
  186.             MemoryStream _stream = new MemoryStream();
  187.             int Length = 256;
  188.             Byte[] buffer = new Byte[Length];
  189.             int bytesRead = streamResponse.Read(buffer, 0, Length);
  190.             while (bytesRead > 0)
  191.             {
  192.                 _stream.Write(buffer, 0, bytesRead);
  193.                 bytesRead = streamResponse.Read(buffer, 0, Length);
  194.             }
  195.             return _stream;
  196.         }
  197.         #endregion

  198.         #region SetRequest

  199.         /// <summary>
  200.         /// 为请求准备参数
  201.         /// </summary>
  202.         ///<param name="item">参数列表</param>
  203.         private void SetRequest(HttpItem item)
  204.         {
  205.             // 验证证书
  206.             SetCer(item);
  207.             //设置Header参数
  208.             if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
  209.                 {
  210.                     request.Headers.Add(key, item.Header[key]);
  211.                 }
  212.             // 设置代理
  213.             SetProxy(item);
  214.             if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
  215.             request.ServicePoint.Expect100Continue = item.Expect100Continue;
  216.             //请求方式Get或者Post
  217.             request.Method = item.Method;
  218.             request.Timeout = item.Timeout;
  219.             request.KeepAlive = item.KeepAlive;
  220.             request.ReadWriteTimeout = item.ReadWriteTimeout;
  221.             if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
  222.             //Accept
  223.             request.Accept = item.Accept;
  224.             //ContentType返回类型
  225.             request.ContentType = item.ContentType;
  226.             //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
  227.             request.UserAgent = item.UserAgent;
  228.             // 编码
  229.             encoding = item.Encoding;
  230.             //设置安全凭证
  231.             request.Credentials = item.ICredentials;
  232.             //设置Cookie
  233.             SetCookie(item);
  234.             //来源地址
  235.             request.Referer = item.Referer;
  236.             //是否执行跳转功能
  237.             request.AllowAutoRedirect = item.Allowautoredirect;
  238.             if (item.MaximumAutomaticRedirections > 0)
  239.             {
  240.                 request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;
  241.             }
  242.             //设置Post数据
  243.             SetPostData(item);
  244.             //设置最大连接
  245.             if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
  246.         }
  247.         /// <summary>
  248.         /// 设置证书
  249.         /// </summary>
  250.         /// <param name="item"></param>
  251.         private void SetCer(HttpItem item)
  252.         {
  253.             if (!string.IsNullOrEmpty(item.CerPath))
  254.             {
  255.                 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
  256.                 ServicePointManager.ServerCertificateValIDAtionCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  257.                 //初始化对像,并设置请求的URL地址
  258.                 request = (HttpWebRequest)WebRequest.Create(item.URL);
  259.                 SetCerList(item);
  260.                 //将证书添加到请求里
  261.                 request.ClientCertificates.Add(new X509Certificate(item.CerPath));
  262.             }
  263.             else
  264.             {
  265.                 //初始化对像,并设置请求的URL地址
  266.                 request = (HttpWebRequest)WebRequest.Create(item.URL);
  267.                 SetCerList(item);
  268.             }
  269.         }
  270.         /// <summary>
  271.         /// 设置多个证书
  272.         /// </summary>
  273.         /// <param name="item"></param>
  274.         private void SetCerList(HttpItem item)
  275.         {
  276.             if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
  277.             {
  278.                 foreach (X509Certificate c in item.ClentCertificates)
  279.                 {
  280.                     request.ClientCertificates.Add(c);
  281.                 }
  282.             }
  283.         }
  284.         /// <summary>
  285.         /// 设置Cookie
  286.         /// </summary>
  287.         /// <param name="item">Http参数</param>
  288.         private void SetCookie(HttpItem item)
  289.         {
  290.             if (!string.IsNullOrEmpty(item.Cookie)) request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
  291.             //设置CookieCollection
  292.             if (item.ResultCookieType == ResultCookieType.CookieCollection)
  293.             {
  294.                 request.CookieContainer = new CookieContainer();
  295.                 if (item.CookieCollection != null && item.CookieCollection.Count > 0)
  296.                     request.CookieContainer.Add(item.CookieCollection);
  297.             }
  298.         }
  299.         /// <summary>
  300.         /// 设置Post数据
  301.         /// </summary>
  302.         /// <param name="item">Http参数</param>
  303.         private void SetPostData(HttpItem item)
  304.         {
  305.             //验证在得到结果时是否有传入数据
  306.             if (!request.Method.Trim().ToLower().Contains("get"))
  307.             {
  308.                 if (item.PostEncoding != null)
  309.                 {
  310.                     postencoding = item.PostEncoding;
  311.                 }
  312.                 byte[] buffer = null;
  313.                 //写入Byte类型
  314.                 if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
  315.                 {
  316.                     //验证在得到结果时是否有传入数据
  317.                     buffer = item.PostdataByte;
  318.                 }//写入文件
  319.                 else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
  320.                 {
  321.                     StreamReader r = new StreamReader(item.Postdata, postencoding);
  322.                     buffer = postencoding.GetBytes(r.ReadToEnd());
  323.                     r.Close();
  324.                 } //写入字符串
  325.                 else if (!string.IsNullOrEmpty(item.Postdata))
  326.                 {
  327.                     buffer = postencoding.GetBytes(item.Postdata);
  328.                 }
  329.                 if (buffer != null)
  330.                 {
  331.                     request.ContentLength = buffer.Length;
  332.                     request.GetRequestStream().Write(buffer, 0, buffer.Length);
  333.                 }
  334.             }
  335.         }
  336.         /// <summary>
  337.         /// 设置代理
  338.         /// </summary>
  339.         /// <param name="item">参数对象</param>
  340.         private void SetProxy(HttpItem item)
  341.         {
  342.             bool isIeProxy = false;
  343.             if (!string.IsNullOrEmpty(item.ProxyIp))
  344.             {
  345.                 isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy");
  346.             }
  347.             if (!string.IsNullOrEmpty(item.ProxyIp) && !isIeProxy)
  348.             {
  349.                 //设置代理服务器
  350.                 if (item.ProxyIp.Contains(":"))
  351.                 {
  352.                     string[] plist = item.ProxyIp.Split(':');
  353.                     WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
  354.                     //建议连接
  355.                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
  356.                     //给当前请求对象
  357.                     request.Proxy = myProxy;
  358.                 }
  359.                 else
  360.                 {
  361.                     WebProxy myProxy = new WebProxy(item.ProxyIp, false);
  362.                     //建议连接
  363.                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
  364.                     //给当前请求对象
  365.                     request.Proxy = myProxy;
  366.                 }
  367.             }
  368.             else if (isIeProxy)
  369.             {
  370.                 //设置为IE代理
  371.             }
  372.             else
  373.             {
  374.                 request.Proxy = item.WebProxy;
  375.             }
  376.         }
  377.         #endregion

  378.         #region private main
  379.         /// <summary>
  380.         /// 回调验证证书问题
  381.         /// </summary>
  382.         /// <param name="sender">流对象</param>
  383.         /// <param name="certificate">证书</param>
  384.         /// <param name="chain">X509Chain</param>
  385.         /// <param name="errors">SslPolicyErrors</param>
  386.         /// <returns>bool</returns>
  387.         private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
  388.         #endregion
  389.     }
  390.     /// <summary>
  391.     /// Http请求参考类
  392.     /// </summary>
  393.     public class HttpItem
  394.     {
  395.         string _URL = string.Empty;
  396.         /// <summary>
  397.         /// 请求URL必须填写
  398.         /// </summary>
  399.         public string URL
  400.         {
  401.             get { return _URL; }
  402.             set { _URL = value; }
  403.         }
  404.         string _Method = "GET";
  405.         /// <summary>
  406.         /// 请求方式默认为GET方式,当为POST方式时必须设置Postdata的值
  407.         /// </summary>
  408.         public string Method
  409.         {
  410.             get { return _Method; }
  411.             set { _Method = value; }
  412.         }
  413.         int _Timeout = 100000;
  414.         /// <summary>
  415.         /// 默认请求超时时间
  416.         /// </summary>
  417.         public int Timeout
  418.         {
  419.             get { return _Timeout; }
  420.             set { _Timeout = value; }
  421.         }
  422.         int _ReadWriteTimeout = 30000;
  423.         /// <summary>
  424.         /// 默认写入Post数据超时间
  425.         /// </summary>
  426.         public int ReadWriteTimeout
  427.         {
  428.             get { return _ReadWriteTimeout; }
  429.             set { _ReadWriteTimeout = value; }
  430.         }
  431.         Boolean _KeepAlive = true;
  432.         /// <summary>
  433.         ///  获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接默认为true。
  434.         /// </summary>
  435.         public Boolean KeepAlive
  436.         {
  437.             get { return _KeepAlive; }
  438.             set { _KeepAlive = value; }
  439.         }
  440.         string _Accept = "text/html, application/xhtml+xml, */*";
  441.         /// <summary>
  442.         /// 请求标头值 默认为text/html, application/xhtml+xml, */*
  443.         /// </summary>
  444.         public string Accept
  445.         {
  446.             get { return _Accept; }
  447.             set { _Accept = value; }
  448.         }
  449.         string _ContentType = "text/html";
  450.         /// <summary>
  451.         /// 请求返回类型默认 text/html
  452.         /// </summary>
  453.         public string ContentType
  454.         {
  455.             get { return _ContentType; }
  456.             set { _ContentType = value; }
  457.         }
  458.         string _UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
  459.         /// <summary>
  460.         /// 客户端访问信息默认Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
  461.         /// </summary>
  462.         public string UserAgent
  463.         {
  464.             get { return _UserAgent; }
  465.             set { _UserAgent = value; }
  466.         }
  467.         Encoding _Encoding = null;
  468.         /// <summary>
  469.         /// 返回数据编码默认为NUll,可以自动识别,一般为utf-8,gbk,gb2312
  470.         /// </summary>
  471.         public Encoding Encoding
  472.         {
  473.             get { return _Encoding; }
  474.             set { _Encoding = value; }
  475.         }
  476.         private PostDataType _PostDataType = PostDataType.String;
  477.         /// <summary>
  478.         /// Post的数据类型
  479.         /// </summary>
  480.         public PostDataType PostDataType
  481.         {
  482.             get { return _PostDataType; }
  483.             set { _PostDataType = value; }
  484.         }
  485.         string _Postdata = string.Empty;
  486.         /// <summary>
  487.         /// Post请求时要发送的字符串Post数据
  488.         /// </summary>
  489.         public string Postdata
  490.         {
  491.             get { return _Postdata; }
  492.             set { _Postdata = value; }
  493.         }
  494.         private byte[] _PostdataByte = null;
  495.         /// <summary>
  496.         /// Post请求时要发送的Byte类型的Post数据
  497.         /// </summary>
  498.         public byte[] PostdataByte
  499.         {
  500.             get { return _PostdataByte; }
  501.             set { _PostdataByte = value; }
  502.         }
  503.         private WebProxy _WebProxy;
  504.         /// <summary>
  505.         /// 设置代理对象,不想使用IE默认配置就设置为Null,而且不要设置ProxyIp
  506.         /// </summary>
  507.         public WebProxy WebProxy
  508.         {
  509.             get { return _WebProxy; }
  510.             set { _WebProxy = value; }
  511.         }

  512.         CookieCollection cookiecollection = null;
  513.         /// <summary>
  514.         /// Cookie对象集合
  515.         /// </summary>
  516.         public CookieCollection CookieCollection
  517.         {
  518.             get { return cookiecollection; }
  519.             set { cookiecollection = value; }
  520.         }
  521.         string _Cookie = string.Empty;
  522.         /// <summary>
  523.         /// 请求时的Cookie
  524.         /// </summary>
  525.         public string Cookie
  526.         {
  527.             get { return _Cookie; }
  528.             set { _Cookie = value; }
  529.         }
  530.         string _Referer = string.Empty;
  531.         /// <summary>
  532.         /// 来源地址,上次访问地址
  533.         /// </summary>
  534.         public string Referer
  535.         {
  536.             get { return _Referer; }
  537.             set { _Referer = value; }
  538.         }
  539.         string _CerPath = string.Empty;
  540.         /// <summary>
  541.         /// 证书绝对路径
  542.         /// </summary>
  543.         public string CerPath
  544.         {
  545.             get { return _CerPath; }
  546.             set { _CerPath = value; }
  547.         }
  548.         private Boolean isToLower = false;
  549.         /// <summary>
  550.         /// 是否设置为全文小写,默认为不转化
  551.         /// </summary>
  552.         public Boolean IsToLower
  553.         {
  554.             get { return isToLower; }
  555.             set { isToLower = value; }
  556.         }
  557.         private Boolean allowautoredirect = false;
  558.         /// <summary>
  559.         /// 支持跳转页面,查询结果将是跳转后的页面,默认是不跳转
  560.         /// </summary>
  561.         public Boolean Allowautoredirect
  562.         {
  563.             get { return allowautoredirect; }
  564.             set { allowautoredirect = value; }
  565.         }
  566.         private int connectionlimit = 1024;
  567.         /// <summary>
  568.         /// 最大连接数
  569.         /// </summary>
  570.         public int Connectionlimit
  571.         {
  572.             get { return connectionlimit; }
  573.             set { connectionlimit = value; }
  574.         }
  575.         private string proxyusername = string.Empty;
  576.         /// <summary>
  577.         /// 代理Proxy 服务器用户名
  578.         /// </summary>
  579.         public string ProxyUserName
  580.         {
  581.             get { return proxyusername; }
  582.             set { proxyusername = value; }
  583.         }
  584.         private string proxypwd = string.Empty;
  585.         /// <summary>
  586.         /// 代理 服务器密码
  587.         /// </summary>
  588.         public string ProxyPwd
  589.         {
  590.             get { return proxypwd; }
  591.             set { proxypwd = value; }
  592.         }
  593.         private string proxyip = string.Empty;
  594.         /// <summary>
  595.         /// 代理 服务IP ,如果要使用IE代理就设置为ieproxy
  596.         /// </summary>
  597.         public string ProxyIp
  598.         {
  599.             get { return proxyip; }
  600.             set { proxyip = value; }
  601.         }
  602.         private ResultType resulttype = ResultType.String;
  603.         /// <summary>
  604.         /// 设置返回类型String和Byte
  605.         /// </summary>
  606.         public ResultType ResultType
  607.         {
  608.             get { return resulttype; }
  609.             set { resulttype = value; }
  610.         }
  611.         private WebHeaderCollection header = new WebHeaderCollection();
  612.         /// <summary>
  613.         /// header对象
  614.         /// </summary>
  615.         public WebHeaderCollection Header
  616.         {
  617.             get { return header; }
  618.             set { header = value; }
  619.         }

  620.         private Version _ProtocolVersion;

  621.         /// <summary>
  622.         //     获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。
  623.         /// </summary>
  624.         public Version ProtocolVersion
  625.         {
  626.             get { return _ProtocolVersion; }
  627.             set { _ProtocolVersion = value; }
  628.         }
  629.         private Boolean _expect100continue = true;
  630.         /// <summary>
  631.         ///  获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。
  632.         /// </summary>
  633.         public Boolean Expect100Continue
  634.         {
  635.             get { return _expect100continue; }
  636.             set { _expect100continue = value; }
  637.         }
  638.         private X509CertificateCollection _ClentCertificates;
  639.         /// <summary>
  640.         /// 设置509证书集合
  641.         /// </summary>
  642.         public X509CertificateCollection ClentCertificates
  643.         {
  644.             get { return _ClentCertificates; }
  645.             set { _ClentCertificates = value; }
  646.         }
  647.         private Encoding _PostEncoding;
  648.         /// <summary>
  649.         /// 设置或获取Post参数编码,默认的为Default编码
  650.         /// </summary>
  651.         public Encoding PostEncoding
  652.         {
  653.             get { return _PostEncoding; }
  654.             set { _PostEncoding = value; }
  655.         }
  656.         private ResultCookieType _ResultCookieType = ResultCookieType.String;
  657.         /// <summary>
  658.         /// Cookie返回类型,默认的是只返回字符串类型
  659.         /// </summary>
  660.         public ResultCookieType ResultCookieType
  661.         {
  662.             get { return _ResultCookieType; }
  663.             set { _ResultCookieType = value; }
  664.         }

  665.         private ICredentials _ICredentials = CredentialCache.DefaultCredentials;
  666.         /// <summary>
  667.         /// 获取或设置请求的身份验证信息。
  668.         /// </summary>
  669.         public ICredentials ICredentials
  670.         {
  671.             get { return _ICredentials; }
  672.             set { _ICredentials = value; }
  673.         }
  674.         /// <summary>
  675.         /// 设置请求将跟随的重定向的最大数目
  676.         /// </summary>
  677.         private int _MaximumAutomaticRedirections;

  678.         public int MaximumAutomaticRedirections
  679.         {
  680.             get { return _MaximumAutomaticRedirections; }
  681.             set { _MaximumAutomaticRedirections = value; }
  682.         }

  683.         private DateTime? _IfModifiedSince = null;
  684.         /// <summary>
  685.         /// 获取和设置IfModifiedSince,默认为当前日期和时间
  686.         /// </summary>
  687.         public DateTime? IfModifiedSince
  688.         {
  689.             get { return _IfModifiedSince; }
  690.             set { _IfModifiedSince = value; }
  691.         }

  692.     }
  693.     /// <summary>
  694.     /// Http返回参数类
  695.     /// </summary>
  696.     public class HttpResult
  697.     {
  698.         private string _Cookie;
  699.         /// <summary>
  700.         /// Http请求返回的Cookie
  701.         /// </summary>
  702.         public string Cookie
  703.         {
  704.             get { return _Cookie; }
  705.             set { _Cookie = value; }
  706.         }

  707.         private CookieCollection _CookieCollection;
  708.         /// <summary>
  709.         /// Cookie对象集合
  710.         /// </summary>
  711.         public CookieCollection CookieCollection
  712.         {
  713.             get { return _CookieCollection; }
  714.             set { _CookieCollection = value; }
  715.         }
  716.         private string _html = string.Empty;
  717.         /// <summary>
  718.         /// 返回的String类型数据 只有ResultType.String时才返回数据,其它情况为空
  719.         /// </summary>
  720.         public string Html
  721.         {
  722.             get { return _html; }
  723.             set { _html = value; }
  724.         }
  725.         private byte[] _ResultByte;
  726.         /// <summary>
  727.         /// 返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空
  728.         /// </summary>
  729.         public byte[] ResultByte
  730.         {
  731.             get { return _ResultByte; }
  732.             set { _ResultByte = value; }
  733.         }
  734.         private WebHeaderCollection _Header;
  735.         /// <summary>
  736.         /// header对象
  737.         /// </summary>
  738.         public WebHeaderCollection Header
  739.         {
  740.             get { return _Header; }
  741.             set { _Header = value; }
  742.         }
  743.         private string _StatusDescription;
  744.         /// <summary>
  745.         /// 返回状态说明
  746.         /// </summary>
  747.         public string StatusDescription
  748.         {
  749.             get { return _StatusDescription; }
  750.             set { _StatusDescription = value; }
  751.         }
  752.         private HttpStatusCode _StatusCode;
  753.         /// <summary>
  754.         /// 返回状态码,默认为OK
  755.         /// </summary>
  756.         public HttpStatusCode StatusCode
  757.         {
  758.             get { return _StatusCode; }
  759.             set { _StatusCode = value; }
  760.         }
  761.     }
  762.     /// <summary>
  763.     /// 返回类型
  764.     /// </summary>
  765.     public enum ResultType
  766.     {
  767.         /// <summary>
  768.         /// 表示只返回字符串 只有Html有数据
  769.         /// </summary>
  770.         String,
  771.         /// <summary>
  772.         /// 表示返回字符串和字节流 ResultByte和Html都有数据返回
  773.         /// </summary>
  774.         Byte
  775.     }
  776.     /// <summary>
  777.     /// Post的数据格式默认为string
  778.     /// </summary>
  779.     public enum PostDataType
  780.     {
  781.         /// <summary>
  782.         /// 字符串类型,这时编码Encoding可不设置
  783.         /// </summary>
  784.         String,
  785.         /// <summary>
  786.         /// Byte类型,需要设置PostdataByte参数的值编码Encoding可设置为空
  787.         /// </summary>
  788.         Byte,
  789.         /// <summary>
  790.         /// 传文件,Postdata必须设置为文件的绝对路径,必须设置Encoding的值
  791.         /// </summary>
  792.         FilePath
  793.     }
  794.     /// <summary>
  795.     /// Cookie返回类型
  796.     /// </summary>
  797.     public enum ResultCookieType
  798.     {
  799.         /// <summary>
  800.         /// 只返回字符串类型的Cookie
  801.         /// </summary>
  802.         String,
  803.         /// <summary>
  804.         /// CookieCollection格式的Cookie集合同时也返回String类型的cookie
  805.         /// </summary>
  806.         CookieCollection
  807.     }
复制代码




联系我时,请说是在 挂海论坛 上看到的,谢谢!

免费评分

参与人数 1海币 +1 收起 理由
Crook + 1 精彩文章希望继续努力



上一篇:数据结构与算法分析(C#版):学习数据结构的东东
下一篇:.net 开发游戏 有什么优势和劣势???
免责声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关。一切关于该内容及资源商业行为与www.52ghai.com无关。

2、本站提供的一切资源内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。

3、本站信息来自第三方用户,非本站自制,版权归原作者享有,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。

4、如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵犯你版权的,请邮件与我们联系删除(邮箱:[email protected]),本站将立即改正。

9

积分

1

主题

0

听众
已帮网友解决0 个问题
好评
0
贡献
8
海币
9
交易币
0
沙发
 楼主| 发表于 2016-3-30 14:57:45 | 只看该作者
支持楼主,感谢楼主的分享,好贴必须学习!

191

积分

1

主题

4

听众
已帮网友解决0 个问题
好评
0
贡献
190
海币
367
交易币
0
板凳
发表于 2016-3-30 16:05:03 | 只看该作者
打酱油的啦,飘过赚点海币而已。

1

积分

0

主题

1

听众
已帮网友解决0 个问题
好评
0
贡献
1
海币
11
交易币
0
地板
发表于 2016-9-2 22:38:33 | 只看该作者
支持楼主,感谢楼主的分享,好贴必须学习!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

免责声明|Archiver|手机版|小黑屋|挂海论坛

GMT+8, 2024-5-6 21:21 , Processed in 0.446820 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.2

本站资源来自互联网用户收集发布,如有侵权请邮件与我们联系处理。xhzlw@foxmail.com

快速回复 返回顶部 返回列表