httpHelper.cs 30 KB

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