httpHelper.cs 31 KB

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