HttpHelper4.0.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. using System.Linq;
  11. using System.Net.Cache;
  12. namespace WebApp.CHttp_SuFei2
  13. {
  14. /// <summary>
  15. /// Http连接操作帮助类4.0(版本1.6)
  16. /// </summary>
  17. public class CHttpHelper
  18. {
  19. #region 预定义方变量
  20. //默认的编码
  21. private Encoding encoding = Encoding.Default;
  22. //Post数据编码
  23. private Encoding postencoding = Encoding.Default;
  24. //HttpWebRequest对象用来发起请求
  25. private HttpWebRequest request = null;
  26. //获取影响流的数据对象
  27. private HttpWebResponse response = null;
  28. //设置本地的出口ip和端口
  29. private IPEndPoint _IPEndPoint = null;
  30. #endregion
  31. #region Public
  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. //配置参数时出错
  49. return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message };
  50. }
  51. try
  52. {
  53. //请求数据
  54. using (response = (HttpWebResponse)request.GetResponse())
  55. {
  56. GetData(item, result);
  57. }
  58. }
  59. catch (WebException ex)
  60. {
  61. if (ex.Response != null)
  62. {
  63. using (response = (HttpWebResponse)ex.Response)
  64. {
  65. GetData(item, result);
  66. }
  67. }
  68. else
  69. {
  70. result.Html = ex.Message;
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. result.Html = ex.Message;
  76. }
  77. if (item.IsToLower) result.Html = result.Html.ToLower();
  78. return result;
  79. }
  80. #endregion
  81. #region GetData
  82. /// <summary>
  83. /// 获取数据的并解析的方法
  84. /// </summary>
  85. /// <param name="item"></param>
  86. /// <param name="result"></param>
  87. private void GetData(HttpItem item, HttpResult result)
  88. {
  89. if (response == null)
  90. {
  91. return;
  92. }
  93. #region base
  94. //获取StatusCode
  95. result.StatusCode = response.StatusCode;
  96. //获取StatusDescription
  97. result.StatusDescription = response.StatusDescription;
  98. //获取Headers
  99. result.Header = response.Headers;
  100. //获取最后访问的URl
  101. result.ResponseUri = response.ResponseUri.ToString();
  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. using (MemoryStream _stream = new MemoryStream())
  184. {
  185. //GZIIP处理
  186. if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
  187. {
  188. //开始读取流并设置编码方式
  189. new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
  190. }
  191. else
  192. {
  193. //开始读取流并设置编码方式
  194. response.GetResponseStream().CopyTo(_stream, 10240);
  195. }
  196. //获取Byte
  197. ResponseByte = _stream.ToArray();
  198. }
  199. return ResponseByte;
  200. }
  201. #endregion
  202. #region SetRequest
  203. /// <summary>
  204. /// 为请求准备参数
  205. /// </summary>
  206. ///<param name="item">参数列表</param>
  207. private void SetRequest(HttpItem item)
  208. {
  209. // 验证证书
  210. SetCer(item);
  211. if (item.IPEndPoint != null)
  212. {
  213. _IPEndPoint = item.IPEndPoint;
  214. //设置本地的出口ip和端口
  215. request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
  216. }
  217. //设置Header参数
  218. if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
  219. {
  220. request.Headers.Add(key, item.Header[key]);
  221. }
  222. // 设置代理
  223. SetProxy(item);
  224. if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
  225. request.ServicePoint.Expect100Continue = item.Expect100Continue;
  226. //请求方式Get或者Post
  227. request.Method = item.Method;
  228. request.Timeout = item.Timeout;
  229. request.KeepAlive = item.KeepAlive;
  230. request.ReadWriteTimeout = item.ReadWriteTimeout;
  231. if (!string.IsNullOrWhiteSpace(item.Host))
  232. {
  233. request.Host = item.Host;
  234. }
  235. if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
  236. //Accept
  237. request.Accept = item.Accept;
  238. //ContentType返回类型
  239. request.ContentType = item.ContentType;
  240. //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
  241. request.UserAgent = item.UserAgent;
  242. // 编码
  243. encoding = item.Encoding;
  244. //设置安全凭证
  245. request.Credentials = item.ICredentials;
  246. //设置Cookie
  247. SetCookie(item);
  248. //来源地址
  249. request.Referer = item.Referer;
  250. //是否执行跳转功能
  251. request.AllowAutoRedirect = item.Allowautoredirect;
  252. if (item.MaximumAutomaticRedirections > 0)
  253. {
  254. request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;
  255. }
  256. //设置Post数据
  257. SetPostData(item);
  258. //设置最大连接
  259. if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
  260. }
  261. /// <summary>
  262. /// 设置证书
  263. /// </summary>
  264. /// <param name="item"></param>
  265. private void SetCer(HttpItem item)
  266. {
  267. if (!string.IsNullOrWhiteSpace(item.CerPath))
  268. {
  269. //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
  270. ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  271. //初始化对像,并设置请求的URL地址
  272. request = (HttpWebRequest)WebRequest.Create(item.URL);
  273. SetCerList(item);
  274. //将证书添加到请求里
  275. request.ClientCertificates.Add(new X509Certificate(item.CerPath));
  276. }
  277. else
  278. {
  279. //初始化对像,并设置请求的URL地址
  280. request = (HttpWebRequest)WebRequest.Create(item.URL);
  281. SetCerList(item);
  282. }
  283. }
  284. /// <summary>
  285. /// 设置多个证书
  286. /// </summary>
  287. /// <param name="item"></param>
  288. private void SetCerList(HttpItem item)
  289. {
  290. if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
  291. {
  292. foreach (X509Certificate c in item.ClentCertificates)
  293. {
  294. request.ClientCertificates.Add(c);
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// 设置Cookie
  300. /// </summary>
  301. /// <param name="item">Http参数</param>
  302. private void SetCookie(HttpItem item)
  303. {
  304. if (!string.IsNullOrEmpty(item.Cookie)) request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
  305. //设置CookieCollection
  306. if (item.ResultCookieType == ResultCookieType.CookieCollection)
  307. {
  308. request.CookieContainer = new CookieContainer();
  309. if (item.CookieCollection != null && item.CookieCollection.Count > 0)
  310. request.CookieContainer.Add(item.CookieCollection);
  311. }
  312. }
  313. /// <summary>
  314. /// 设置Post数据
  315. /// </summary>
  316. /// <param name="item">Http参数</param>
  317. private void SetPostData(HttpItem item)
  318. {
  319. //验证在得到结果时是否有传入数据
  320. if (!request.Method.Trim().ToLower().Contains("get"))
  321. {
  322. if (item.PostEncoding != null)
  323. {
  324. postencoding = item.PostEncoding;
  325. }
  326. byte[] buffer = null;
  327. //写入Byte类型
  328. if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
  329. {
  330. //验证在得到结果时是否有传入数据
  331. buffer = item.PostdataByte;
  332. }//写入文件
  333. else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(item.Postdata))
  334. {
  335. StreamReader r = new StreamReader(item.Postdata, postencoding);
  336. buffer = postencoding.GetBytes(r.ReadToEnd());
  337. r.Close();
  338. } //写入字符串
  339. else if (!string.IsNullOrWhiteSpace(item.Postdata))
  340. {
  341. buffer = postencoding.GetBytes(item.Postdata);
  342. }
  343. if (buffer != null)
  344. {
  345. request.ContentLength = buffer.Length;
  346. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  347. }
  348. }
  349. }
  350. /// <summary>
  351. /// 设置代理
  352. /// </summary>
  353. /// <param name="item">参数对象</param>
  354. private void SetProxy(HttpItem item)
  355. {
  356. bool isIeProxy = false;
  357. if (!string.IsNullOrWhiteSpace(item.ProxyIp))
  358. {
  359. isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy");
  360. }
  361. if (!string.IsNullOrWhiteSpace(item.ProxyIp) && !isIeProxy)
  362. {
  363. //设置代理服务器
  364. if (item.ProxyIp.Contains(":"))
  365. {
  366. string[] plist = item.ProxyIp.Split(':');
  367. WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
  368. //建议连接
  369. myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
  370. //给当前请求对象
  371. request.Proxy = myProxy;
  372. }
  373. else
  374. {
  375. WebProxy myProxy = new WebProxy(item.ProxyIp, false);
  376. //建议连接
  377. myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
  378. //给当前请求对象
  379. request.Proxy = myProxy;
  380. }
  381. }
  382. else if (isIeProxy)
  383. {
  384. //设置为IE代理
  385. }
  386. else
  387. {
  388. request.Proxy = item.WebProxy;
  389. }
  390. }
  391. #endregion
  392. #region private main
  393. /// <summary>
  394. /// 回调验证证书问题
  395. /// </summary>
  396. /// <param name="sender">流对象</param>
  397. /// <param name="certificate">证书</param>
  398. /// <param name="chain">X509Chain</param>
  399. /// <param name="errors">SslPolicyErrors</param>
  400. /// <returns>bool</returns>
  401. private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
  402. /// <summary>
  403. /// 通过设置这个属性,可以在发出连接的时候绑定客户端发出连接所使用的IP地址。
  404. /// </summary>
  405. /// <param name="servicePoint"></param>
  406. /// <param name="remoteEndPoint"></param>
  407. /// <param name="retryCount"></param>
  408. /// <returns></returns>
  409. private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
  410. {
  411. return _IPEndPoint;//端口号
  412. }
  413. #endregion
  414. }
  415. #region public calss
  416. /// <summary>
  417. /// Http请求参考类
  418. /// </summary>
  419. public class HttpItem
  420. {
  421. /// <summary>
  422. /// 请求URL必须填写
  423. /// </summary>
  424. public string URL { get; set; }
  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. /// <summary>
  453. /// 设置Host的标头信息
  454. /// </summary>
  455. public string Host { get; set; }
  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. /// <summary>
  493. /// 返回数据编码默认为NUll,可以自动识别,一般为utf-8,gbk,gb2312
  494. /// </summary>
  495. public Encoding Encoding { get; set; }
  496. private PostDataType _PostDataType = PostDataType.String;
  497. /// <summary>
  498. /// Post的数据类型
  499. /// </summary>
  500. public PostDataType PostDataType
  501. {
  502. get { return _PostDataType; }
  503. set { _PostDataType = value; }
  504. }
  505. /// <summary>
  506. /// Post请求时要发送的字符串Post数据
  507. /// </summary>
  508. public string Postdata { get; set; }
  509. /// <summary>
  510. /// Post请求时要发送的Byte类型的Post数据
  511. /// </summary>
  512. public byte[] PostdataByte { get; set; }
  513. /// <summary>
  514. /// Cookie对象集合
  515. /// </summary>
  516. public CookieCollection CookieCollection { get; set; }
  517. /// <summary>
  518. /// 请求时的Cookie
  519. /// </summary>
  520. public string Cookie { get; set; }
  521. /// <summary>
  522. /// 来源地址,上次访问地址
  523. /// </summary>
  524. public string Referer { get; set; }
  525. /// <summary>
  526. /// 证书绝对路径
  527. /// </summary>
  528. public string CerPath { get; set; }
  529. /// <summary>
  530. /// 设置代理对象,不想使用IE默认配置就设置为Null,而且不要设置ProxyIp
  531. /// </summary>
  532. public WebProxy WebProxy { get; set; }
  533. private Boolean isToLower = false;
  534. /// <summary>
  535. /// 是否设置为全文小写,默认为不转化
  536. /// </summary>
  537. public Boolean IsToLower
  538. {
  539. get { return isToLower; }
  540. set { isToLower = value; }
  541. }
  542. private Boolean allowautoredirect = false;
  543. /// <summary>
  544. /// 支持跳转页面,查询结果将是跳转后的页面,默认是不跳转
  545. /// </summary>
  546. public Boolean Allowautoredirect
  547. {
  548. get { return allowautoredirect; }
  549. set { allowautoredirect = value; }
  550. }
  551. private int connectionlimit = 1024;
  552. /// <summary>
  553. /// 最大连接数
  554. /// </summary>
  555. public int Connectionlimit
  556. {
  557. get { return connectionlimit; }
  558. set { connectionlimit = value; }
  559. }
  560. /// <summary>
  561. /// 代理Proxy 服务器用户名
  562. /// </summary>
  563. public string ProxyUserName { get; set; }
  564. /// <summary>
  565. /// 代理 服务器密码
  566. /// </summary>
  567. public string ProxyPwd { get; set; }
  568. /// <summary>
  569. /// 代理 服务IP,如果要使用IE代理就设置为ieproxy
  570. /// </summary>
  571. public string ProxyIp { get; set; }
  572. private ResultType resulttype = ResultType.String;
  573. /// <summary>
  574. /// 设置返回类型String和Byte
  575. /// </summary>
  576. public ResultType ResultType
  577. {
  578. get { return resulttype; }
  579. set { resulttype = value; }
  580. }
  581. private WebHeaderCollection header = new WebHeaderCollection();
  582. /// <summary>
  583. /// header对象
  584. /// </summary>
  585. public WebHeaderCollection Header
  586. {
  587. get { return header; }
  588. set { header = value; }
  589. }
  590. /// <summary>
  591. // 获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。
  592. /// </summary>
  593. public Version ProtocolVersion { get; set; }
  594. private Boolean _expect100continue = true;
  595. /// <summary>
  596. /// 获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。
  597. /// </summary>
  598. public Boolean Expect100Continue
  599. {
  600. get { return _expect100continue; }
  601. set { _expect100continue = value; }
  602. }
  603. /// <summary>
  604. /// 设置509证书集合
  605. /// </summary>
  606. public X509CertificateCollection ClentCertificates { get; set; }
  607. /// <summary>
  608. /// 设置或获取Post参数编码,默认的为Default编码
  609. /// </summary>
  610. public Encoding PostEncoding { get; set; }
  611. private ResultCookieType _ResultCookieType = ResultCookieType.String;
  612. /// <summary>
  613. /// Cookie返回类型,默认的是只返回字符串类型
  614. /// </summary>
  615. public ResultCookieType ResultCookieType
  616. {
  617. get { return _ResultCookieType; }
  618. set { _ResultCookieType = value; }
  619. }
  620. private ICredentials _ICredentials = CredentialCache.DefaultCredentials;
  621. /// <summary>
  622. /// 获取或设置请求的身份验证信息。
  623. /// </summary>
  624. public ICredentials ICredentials
  625. {
  626. get { return _ICredentials; }
  627. set { _ICredentials = value; }
  628. }
  629. /// <summary>
  630. /// 设置请求将跟随的重定向的最大数目
  631. /// </summary>
  632. public int MaximumAutomaticRedirections { get; set; }
  633. private DateTime? _IfModifiedSince = null;
  634. /// <summary>
  635. /// 获取和设置IfModifiedSince,默认为当前日期和时间
  636. /// </summary>
  637. public DateTime? IfModifiedSince
  638. {
  639. get { return _IfModifiedSince; }
  640. set { _IfModifiedSince = value; }
  641. }
  642. #region ip-port
  643. private IPEndPoint _IPEndPoint = null;
  644. /// <summary>
  645. /// 设置本地的出口ip和端口
  646. /// </summary>]
  647. /// <example>
  648. ///item.IPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.1"),80);
  649. /// </example>
  650. public IPEndPoint IPEndPoint
  651. {
  652. get { return _IPEndPoint; }
  653. set { _IPEndPoint = value; }
  654. }
  655. #endregion
  656. }
  657. /// <summary>
  658. /// Http返回参数类
  659. /// </summary>
  660. public class HttpResult
  661. {
  662. /// <summary>
  663. /// Http请求返回的Cookie
  664. /// </summary>
  665. public string Cookie { get; set; }
  666. /// <summary>
  667. /// Cookie对象集合
  668. /// </summary>
  669. public CookieCollection CookieCollection { get; set; }
  670. private string _html = string.Empty;
  671. /// <summary>
  672. /// 返回的String类型数据 只有ResultType.String时才返回数据,其它情况为空
  673. /// </summary>
  674. public string Html
  675. {
  676. get { return _html; }
  677. set { _html = value; }
  678. }
  679. /// <summary>
  680. /// 返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空
  681. /// </summary>
  682. public byte[] ResultByte { get; set; }
  683. /// <summary>
  684. /// header对象
  685. /// </summary>
  686. public WebHeaderCollection Header { get; set; }
  687. /// <summary>
  688. /// 返回状态说明
  689. /// </summary>
  690. public string StatusDescription { get; set; }
  691. /// <summary>
  692. /// 返回状态码,默认为OK
  693. /// </summary>
  694. public HttpStatusCode StatusCode { get; set; }
  695. /// <summary>
  696. /// 最后访问的URl
  697. /// </summary>
  698. public string ResponseUri { get; set; }
  699. /// <summary>
  700. /// 获取重定向的URl
  701. /// </summary>
  702. public string RedirectUrl
  703. {
  704. get
  705. {
  706. try
  707. {
  708. if (Header != null && Header.Count > 0)
  709. {
  710. if (Header.AllKeys.Any(k => k.ToLower().Contains("location")))
  711. {
  712. string locationurl = Header["location"].ToString().ToLower();
  713. if (!string.IsNullOrWhiteSpace(locationurl))
  714. {
  715. bool b = locationurl.StartsWith("http://") || locationurl.StartsWith("https://");
  716. if (!b)
  717. {
  718. locationurl = new Uri(new Uri(ResponseUri), locationurl).AbsoluteUri;
  719. }
  720. }
  721. return locationurl;
  722. }
  723. }
  724. }
  725. catch { }
  726. return string.Empty;
  727. }
  728. }
  729. }
  730. /// <summary>
  731. /// 返回类型
  732. /// </summary>
  733. public enum ResultType
  734. {
  735. /// <summary>
  736. /// 表示只返回字符串 只有Html有数据
  737. /// </summary>
  738. String,
  739. /// <summary>
  740. /// 表示返回字符串和字节流 ResultByte和Html都有数据返回
  741. /// </summary>
  742. Byte
  743. }
  744. /// <summary>
  745. /// Post的数据格式默认为string
  746. /// </summary>
  747. public enum PostDataType
  748. {
  749. /// <summary>
  750. /// 字符串类型,这时编码Encoding可不设置
  751. /// </summary>
  752. String,
  753. /// <summary>
  754. /// Byte类型,需要设置PostdataByte参数的值编码Encoding可设置为空
  755. /// </summary>
  756. Byte,
  757. /// <summary>
  758. /// 传文件,Postdata必须设置为文件的绝对路径,必须设置Encoding的值
  759. /// </summary>
  760. FilePath
  761. }
  762. /// <summary>
  763. /// Cookie返回类型
  764. /// </summary>
  765. public enum ResultCookieType
  766. {
  767. /// <summary>
  768. /// 只返回字符串类型的Cookie
  769. /// </summary>
  770. String,
  771. /// <summary>
  772. /// CookieCollection格式的Cookie集合同时也返回String类型的cookie
  773. /// </summary>
  774. CookieCollection
  775. }
  776. #endregion
  777. }