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

HttpHelper工具类的更多相关文章

  1. C# http请求工具类

    /// <summary> /// Http请求操作类之HttpWebRequest /// </summary> public class HttpHelper { #reg ...

  2. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  3. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  4. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  5. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  6. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  7. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  8. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  9. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

随机推荐

  1. Jenkins错误“editable email notification aborted due to exception”的问题解决

    如果出现:“editable email notification aborted due to exception”这样的错误,尝试升级一下jenkins,估计是这个导致的. 解决思路: http: ...

  2. wxPython学习笔记(三)

    要理解事件,我们需要知道哪些术语? 事件(event):在你的应用程序期间发生的事情,它要求有一个响应. 事件对象(event object):在wxPython中,它具体代表一个事件,其中包括了事件 ...

  3. 微设计(www.weidesigner.com)介绍系列文章(二)

    微设计(www.weidesigner.com)是一个专门针对微信公众账号提供营销推广服务而打造的第三方平台. 2.1 怎样注冊微信公众号? 登录mp.weixin.qq.com,点击注冊填写相关信息 ...

  4. 01标题背包水章 HDU2955——Robberies

    原来是dp[i],它代表的不被抓的概率i这最大的钱抢(可能1-100) 客是dp[i]表示抢了i钱最大的不被抓概率,嗯~,弱菜水题都刷不动. 那么状态转移方程就是 dp[i]=max(dp[i],dp ...

  5. Java基础知识强化89:Date类之Data类概述及其方法

    1. Date类概述 类Date表示特定的瞬间,精确到毫秒 2. 构造方法 public Date():根据当前默认毫秒值创建日期对象 public Date(long date):根据给定的毫秒值创 ...

  6. Mysql group_concat

    select p.id,p.parent_id,group_concat(distinct(CONCAT("分类名称:",c.name)) order by c.id desc s ...

  7. C++线性序列容器<vector>简单总结

    C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...

  8. MySQL TEXT数据类型的最大长度

    TINYTEXT 256 bytes   TEXT 65,535 bytes ~64kb MEDIUMTEXT  16,777,215 bytes ~16MB LONGTEXT 4,294,967,2 ...

  9. Js打开新窗口拦截问题整理

    一.js打开新窗口,经常被拦截 //js打开新窗口,经常被拦截 //指定本窗口打开,可以使用 window.open('http://www.tianma3798.cn', '_self'); //不 ...

  10. (转)select 1 from ... sql语句中的1代表什么意思? .

    select  1 from ..., sql语句中的1代表什么意思?查出来是个什么结果?         select 1 from table;与select anycol(目的表集合中的任意一行 ...