httpHelper.cs 30 KB

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