环境:net framework 4.5.2


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