HttpHelper.cs 31 KB

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