HttpHelper Http/Https請求方式


前言:最近在對接接口時發現其他項目寫的Http/Https請求很多都不能同用(都是按照當時接口進行編寫的),現在整理一份HttpHelper來實現大多數場景。哈哈.... 不講廢話了,代碼貼出來了

 

HttpHelper Http/Https請求方式

1.HttpHelper.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.IO.Compression;
  5 using System.Linq;
  6 using System.Net;
  7 using System.Net.Security;
  8 using System.Security.Cryptography.X509Certificates;
  9 using System.Text;
 10 using System.Text.RegularExpressions;
 11 using System.Threading.Tasks;
 12 
 13 namespace Own.Common
 14 {
 15     /// <summary>
 16     /// Http連接操作幫助類
 17     /// </summary>
 18     public class HttpHelper
 19     {
 20         #region 預定義方法或者變更
 21         //默認的編碼
 22         private Encoding encoding = Encoding.Default;
 23         //Post數據編碼
 24         private Encoding postencoding = Encoding.Default;
 25         //HttpWebRequest對象用來發起請求
 26         private HttpWebRequest request = null;
 27         //獲取影響流的數據對象
 28         private HttpWebResponse response = null;
 29         /// <summary>
 30         /// 根據相傳入的數據,得到相應頁面數據
 31         /// </summary>
 32         /// <param name="item">參數類對象</param>
 33         /// <returns>返回HttpResult類型</returns>
 34         public HttpResult GetHtml(HttpItem item)
 35         {
 36             //返回參數
 37             HttpResult result = new HttpResult();
 38             try
 39             {
 40                 //准備參數
 41                 SetRequest(item);
 42             }
 43             catch (Exception ex)
 44             {
 45                 return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置參數時出錯:" + ex.Message };
 46             }
 47             try
 48             {
 49                 #region 得到請求的response
 50                 if (item.ResultCookieType == ResultCookieType.CookieCollection)
 51                     request.CookieContainer = new CookieContainer();
 52                 using (response = (HttpWebResponse)request.GetResponse())
 53                 {
 54                     result.StatusCode = response.StatusCode;
 55                     result.StatusDescription = response.StatusDescription;
 56                     result.Header = response.Headers;
 57                     if (response.Cookies != null) result.CookieCollection = response.Cookies;
 58                     if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
 59                     byte[] ResponseByte = null;
 60                     using (MemoryStream _stream = new MemoryStream())
 61                     {
 62                         //GZIIP處理
 63                         if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
 64                         {
 65                             //開始讀取流並設置編碼方式
 66                             new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream);
 67                         }
 68                         else
 69                         {
 70                             //開始讀取流並設置編碼方式
 71                             response.GetResponseStream().CopyTo(_stream);
 72                         }
 73                         //獲取Byte
 74                         ResponseByte = _stream.ToArray();
 75                     }
 76                     if (ResponseByte != null & ResponseByte.Length > 0)
 77                     {
 78                         //是否返回Byte類型數據
 79                         if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
 80                         //從這里開始我們要無視編碼了
 81                         if (encoding == null)
 82                         {
 83                             Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
 84                             string c = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty;
 85                             if (c.Length > 2)
 86                             {
 87                                 try
 88                                 {
 89                                     if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" "));
 90                                     encoding = Encoding.GetEncoding(c.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
 91                                 }
 92                                 catch
 93                                 {
 94                                     if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
 95                                     else encoding = Encoding.GetEncoding(response.CharacterSet);
 96                                 }
 97                             }
 98                             else
 99                             {
100                                 if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
101                                 else encoding = Encoding.GetEncoding(response.CharacterSet);
102                             }
103                         }
104                         //得到返回的HTML
105                         result.Html = encoding.GetString(ResponseByte);
106                     }
107                     else
108                     {
109                         //得到返回的HTML
110                         result.Html = "本次請求並未返回任何數據";
111                     }
112                 }
113                 #endregion
114             }
115             catch (WebException ex)
116             {
117                 //這里是在發生異常時返回的錯誤信息
118                 response = (HttpWebResponse)ex.Response;
119                 result.Html = ex.Message;
120                 if (response != null)
121                 {
122                     result.StatusCode = response.StatusCode;
123                     result.StatusDescription = response.StatusDescription;
124                 }
125             }
126             catch (Exception ex)
127             {
128                 result.Html = ex.Message;
129             }
130             if (item.IsToLower) result.Html = result.Html.ToLower();
131             return result;
132         }
133         /// <summary>
134         /// 為請求准備參數
135         /// </summary>
136         ///<param name="item">參數列表</param>
137         private void SetRequest(HttpItem item)
138         {
139             // 驗證證書
140             SetCer(item);
141             //設置Header參數
142             if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
143                 {
144                     request.Headers.Add(key, item.Header[key]);
145                 }
146             // 設置代理
147             SetProxy(item);
148             if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
149             request.ServicePoint.Expect100Continue = item.Expect100Continue;
150             //請求方式Get或者Post
151             request.Method = item.Method;
152             request.Timeout = item.Timeout;
153             request.KeepAlive = item.KeepAlive;
154             request.ReadWriteTimeout = item.ReadWriteTimeout;
155             if (!string.IsNullOrWhiteSpace(item.Host))
156             {
157                 request.Host = item.Host;
158             }
159             //Accept
160             request.Accept = item.Accept;
161             //ContentType返回類型
162             request.ContentType = item.ContentType;
163             //UserAgent客戶端的訪問類型,包括瀏覽器版本和操作系統信息
164             request.UserAgent = item.UserAgent;
165             // 編碼
166             encoding = item.Encoding;
167             //設置Cookie
168             SetCookie(item);
169             //來源地址
170             request.Referer = item.Referer;
171             //是否執行跳轉功能
172             request.AllowAutoRedirect = item.Allowautoredirect;
173             //設置Post數據
174             SetPostData(item);
175             //設置最大連接
176             if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
177         }
178         /// <summary>
179         /// 設置證書
180         /// </summary>
181         /// <param name="item"></param>
182         private void SetCer(HttpItem item)
183         {
184             
185             if (!string.IsNullOrWhiteSpace(item.CerPath))
186             {
187                 //這一句一定要寫在創建連接的前面。使用回調的方法進行證書驗證。
188                 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
189                 //初始化對像,並設置請求的URL地址
190                 request = (HttpWebRequest)WebRequest.Create(item.URL);
191                 SetCerList(item);
192                 //將證書添加到請求里
193                 request.ClientCertificates.Add(new X509Certificate(item.CerPath));
194             }
195             else if (string.IsNullOrWhiteSpace(item.CerPath) && item.URL.IsContains("https"))
196             {
197                 //這一句一定要寫在創建連接的前面。使用回調的方法進行證書驗證。
198                 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
199                 //初始化對像,並設置請求的URL地址
200                 request = (HttpWebRequest)WebRequest.Create(item.URL);
201                 SetCerList(item);
202             }
203             else
204             {
205                 //初始化對像,並設置請求的URL地址
206                 request = (HttpWebRequest)WebRequest.Create(item.URL);
207                 SetCerList(item);
208             }
209         }
210         /// <summary>
211         /// 設置多個證書
212         /// </summary>
213         /// <param name="item"></param>
214         private void SetCerList(HttpItem item)
215         {
216             if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
217             {
218                 foreach (X509Certificate c in item.ClentCertificates)
219                 {
220                     request.ClientCertificates.Add(c);
221                 }
222             }
223         }
224         /// <summary>
225         /// 設置Cookie
226         /// </summary>
227         /// <param name="item">Http參數</param>
228         private void SetCookie(HttpItem item)
229         {
230             if (!string.IsNullOrWhiteSpace(item.Cookie))
231                 //Cookie
232                 request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
233             //設置Cookie
234             if (item.CookieCollection != null && item.CookieCollection.Count > 0)
235             {
236                 request.CookieContainer = new CookieContainer();
237                 request.CookieContainer.Add(item.CookieCollection);
238             }
239         }
240         /// <summary>
241         /// 設置Post數據
242         /// </summary>
243         /// <param name="item">Http參數</param>
244         private void SetPostData(HttpItem item)
245         {
246             //驗證在得到結果時是否有傳入數據
247             if (request.Method.Trim().ToLower().Contains("post"))
248             {
249                 if (item.PostEncoding != null)
250                 {
251                     postencoding = item.PostEncoding;
252                 }
253                 byte[] buffer = null;
254                 //寫入Byte類型
255                 if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
256                 {
257                     //驗證在得到結果時是否有傳入數據
258                     buffer = item.PostdataByte;
259                 }//寫入文件
260                 else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(item.Postdata))
261                 {
262                     StreamReader r = new StreamReader(item.Postdata, postencoding);
263                     buffer = postencoding.GetBytes(r.ReadToEnd());
264                     r.Close();
265                 } //寫入字符串
266                 else if (!string.IsNullOrWhiteSpace(item.Postdata) || item.PostdataDic.Count>0)
267                 {
268                     if(!string.IsNullOrWhiteSpace(item.Postdata))
269                         buffer = postencoding.GetBytes(item.Postdata);
270                     else
271                     {
272                         var strBuffer = new StringBuilder();
273                         var i = 0;
274                         foreach (string key in item.PostdataDic.Keys)
275                         {
276                             if (i > 0)
277                             {
278                                 strBuffer.AppendFormat("&{0}={1}", key, item.PostdataDic[key]);
279                             }
280                             else
281                             {
282                                 strBuffer.AppendFormat("{0}={1}", key, item.PostdataDic[key]);
283                             }
284                             i++;
285                         }
286 
287                         buffer = postencoding.GetBytes(strBuffer.ToString());
288                     }
289                 }
290                 if (buffer != null)
291                 {
292                     request.ContentLength = buffer.Length;
293                     using (var stream = request.GetRequestStream())
294                     {
295                         stream.Write(buffer, 0, buffer.Length);
296                     }
297                 }
298             }
299         }
300         /// <summary>
301         /// 設置代理
302         /// </summary>
303         /// <param name="item">參數對象</param>
304         private void SetProxy(HttpItem item)
305         {
306             if (!string.IsNullOrWhiteSpace(item.ProxyIp))
307             {
308                 //設置代理服務器
309                 if (item.ProxyIp.Contains(":"))
310                 {
311                     string[] plist = item.ProxyIp.Split(':');
312                     WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
313                     //建議連接
314                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
315                     //給當前請求對象
316                     request.Proxy = myProxy;
317                 }
318                 else
319                 {
320                     WebProxy myProxy = new WebProxy(item.ProxyIp, false);
321                     //建議連接
322                     myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
323                     //給當前請求對象
324                     request.Proxy = myProxy;
325                 }
326                 //設置安全憑證
327                 request.Credentials = CredentialCache.DefaultNetworkCredentials;
328             }
329         }
330         /// <summary>
331         /// 回調驗證證書問題
332         /// </summary>
333         /// <param name="sender">流對象</param>
334         /// <param name="certificate">證書</param>
335         /// <param name="chain">X509Chain</param>
336         /// <param name="errors">SslPolicyErrors</param>
337         /// <returns>bool</returns>
338         public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
339         
340         #endregion
341     }
342     /// <summary>
343     /// Http請求參考類
344     /// </summary>
345     public class HttpItem
346     {
347         /// <summary>
348         /// 請求URL必須填寫
349         /// </summary>
350         public string URL { get; set; }
351         string _Method = "GET";
352         /// <summary>
353         /// 請求方式默認為GET方式,當為POST方式時必須設置Postdata的值
354         /// </summary>
355         public string Method
356         {
357             get { return _Method; }
358             set { _Method = value; }
359         }
360         int _Timeout = 100000;
361         /// <summary>
362         /// 默認請求超時時間
363         /// </summary>
364         public int Timeout
365         {
366             get { return _Timeout; }
367             set { _Timeout = value; }
368         }
369         int _ReadWriteTimeout = 30000;
370         /// <summary>
371         /// 默認寫入Post數據超時間
372         /// </summary>
373         public int ReadWriteTimeout
374         {
375             get { return _ReadWriteTimeout; }
376             set { _ReadWriteTimeout = value; }
377         }
378         /// <summary>
379         /// 設置Host的標頭信息
380         /// </summary>
381         public string Host { get; set; }
382         Boolean _KeepAlive = true;
383         /// <summary>
384         ///  獲取或設置一個值,該值指示是否與 Internet 資源建立持久性連接默認為true。
385         /// </summary>
386         public Boolean KeepAlive
387         {
388             get { return _KeepAlive; }
389             set { _KeepAlive = value; }
390         }
391         string _Accept = "text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
392         /// <summary>
393         /// 請求標頭值 默認為text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*
394         /// </summary>
395         public string Accept
396         {
397             get { return _Accept; }
398             set { _Accept = value; }
399         }
400         string _ContentType = "application/x-www-form-urlencoded";
401         /// <summary>
402         /// 請求返回類型默認 
403         /// </summary>
404         public string ContentType
405         {
406             get { return _ContentType; }
407             set { _ContentType = value; }
408         }
409         string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
410         /// <summary>
411         /// 客戶端訪問信息默認Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
412         /// </summary>
413         public string UserAgent
414         {
415             get { return _UserAgent; }
416             set { _UserAgent = value; }
417         }
418         /// <summary>
419         /// 返回數據編碼默認為NUll,可以自動識別,一般為utf-8,gbk,gb2312
420         /// </summary>
421         public Encoding Encoding { get; set; }
422         private PostDataType _PostDataType = PostDataType.String;
423         /// <summary>
424         /// Post的數據類型
425         /// </summary>
426         public PostDataType PostDataType
427         {
428             get { return _PostDataType; }
429             set { _PostDataType = value; }
430         }
431 
432         /// <summary>
433         /// Post請求時要發送的字符串Post數據
434         /// </summary>
435         public string Postdata { get; set; }
436 
437         /// <summary>
438         /// Post請求時要發送的Post數據
439         /// </summary>
440         public IDictionary<string, string> PostdataDic { get; set; }
441 
442         /// <summary>
443         /// Post請求時要發送的Byte類型的Post數據
444         /// </summary>
445         public byte[] PostdataByte { get; set; }
446         /// <summary>
447         /// Cookie對象集合
448         /// </summary>
449         public CookieCollection CookieCollection { get; set; }
450         /// <summary>
451         /// 請求時的Cookie
452         /// </summary>
453         public string Cookie { get; set; }
454         /// <summary>
455         /// 來源地址,上次訪問地址
456         /// </summary>
457         public string Referer { get; set; }
458         /// <summary>
459         /// 證書絕對路徑
460         /// </summary>
461         public string CerPath { get; set; }
462         private Boolean isToLower = false;
463         /// <summary>
464         /// 是否設置為全文小寫,默認為不轉化
465         /// </summary>
466         public Boolean IsToLower
467         {
468             get { return isToLower; }
469             set { isToLower = value; }
470         }
471         private Boolean allowautoredirect = false;
472         /// <summary>
473         /// 支持跳轉頁面,查詢結果將是跳轉后的頁面,默認是不跳轉
474         /// </summary>
475         public Boolean Allowautoredirect
476         {
477             get { return allowautoredirect; }
478             set { allowautoredirect = value; }
479         }
480         private int connectionlimit = 1024;
481         /// <summary>
482         /// 最大連接數
483         /// </summary>
484         public int Connectionlimit
485         {
486             get { return connectionlimit; }
487             set { connectionlimit = value; }
488         }
489         /// <summary>
490         /// 代理Proxy 服務器用戶名
491         /// </summary>
492         public string ProxyUserName { get; set; }
493         /// <summary>
494         /// 代理 服務器密碼
495         /// </summary>
496         public string ProxyPwd { get; set; }
497         /// <summary>
498         /// 代理 服務IP
499         /// </summary>
500         public string ProxyIp { get; set; }
501         private ResultType resulttype = ResultType.String;
502         /// <summary>
503         /// 設置返回類型String和Byte
504         /// </summary>
505         public ResultType ResultType
506         {
507             get { return resulttype; }
508             set { resulttype = value; }
509         }
510         private WebHeaderCollection header = new WebHeaderCollection();
511         /// <summary>
512         /// header對象
513         /// </summary>
514         public WebHeaderCollection Header
515         {
516             get { return header; }
517             set { header = value; }
518         }
519         /// <summary>
520         //     獲取或設置用於請求的 HTTP 版本。返回結果:用於請求的 HTTP 版本。默認為 System.Net.HttpVersion.Version11。
521         /// </summary>
522         public Version ProtocolVersion { get; set; }
523         private Boolean _expect100continue = true;
524         /// <summary>
525         ///  獲取或設置一個 System.Boolean 值,該值確定是否使用 100-Continue 行為。如果 POST 請求需要 100-Continue 響應,則為 true;否則為 false。默認值為 true。
526         /// </summary>
527         public Boolean Expect100Continue
528         {
529             get { return _expect100continue; }
530             set { _expect100continue = value; }
531         }
532         /// <summary>
533         /// 設置509證書集合
534         /// </summary>
535         public X509CertificateCollection ClentCertificates { get; set; }
536         /// <summary>
537         /// 設置或獲取Post參數編碼,默認的為Default編碼
538         /// </summary>
539         public Encoding PostEncoding { get; set; }
540         private ResultCookieType _ResultCookieType = ResultCookieType.String;
541         /// <summary>
542         /// Cookie返回類型,默認的是只返回字符串類型
543         /// </summary>
544         public ResultCookieType ResultCookieType
545         {
546             get { return _ResultCookieType; }
547             set { _ResultCookieType = value; }
548         }
549     }
550     /// <summary>
551     /// Http返回參數類
552     /// </summary>
553     public class HttpResult
554     {
555         /// <summary>
556         /// Http請求返回的Cookie
557         /// </summary>
558         public string Cookie { get; set; }
559         /// <summary>
560         /// Cookie對象集合
561         /// </summary>
562         public CookieCollection CookieCollection { get; set; }
563         /// <summary>
564         /// 返回的String類型數據 只有ResultType.String時才返回數據,其它情況為空
565         /// </summary>
566         public string Html { get; set; }
567         /// <summary>
568         /// 返回的Byte數組 只有ResultType.Byte時才返回數據,其它情況為空
569         /// </summary>
570         public byte[] ResultByte { get; set; }
571         /// <summary>
572         /// header對象
573         /// </summary>
574         public WebHeaderCollection Header { get; set; }
575         /// <summary>
576         /// 返回狀態說明
577         /// </summary>
578         public string StatusDescription { get; set; }
579         /// <summary>
580         /// 返回狀態碼,默認為OK
581         /// </summary>
582         public HttpStatusCode StatusCode { get; set; }
583     }
584     /// <summary>
585     /// 返回類型
586     /// </summary>
587     public enum ResultType
588     {
589         /// <summary>
590         /// 表示只返回字符串 只有Html有數據
591         /// </summary>
592         String,
593         /// <summary>
594         /// 表示返回字符串和字節流 ResultByte和Html都有數據返回
595         /// </summary>
596         Byte
597     }
598     /// <summary>
599     /// Post的數據格式默認為string
600     /// </summary>
601     public enum PostDataType
602     {
603         /// <summary>
604         /// 字符串類型,這時編碼Encoding可不設置
605         /// </summary>
606         String,
607         /// <summary>
608         /// Byte類型,需要設置PostdataByte參數的值編碼Encoding可設置為空
609         /// </summary>
610         Byte,
611         /// <summary>
612         /// 傳文件,Postdata必須設置為文件的絕對路徑,必須設置Encoding的值
613         /// </summary>
614         FilePath
615     }
616     /// <summary>
617     /// Cookie返回類型
618     /// </summary>
619     public enum ResultCookieType
620     {
621         /// <summary>
622         /// 只返回字符串類型的Cookie
623         /// </summary>
624         String,
625         /// <summary>
626         /// CookieCollection格式的Cookie集合同時也返回String類型的cookie
627         /// </summary>
628         CookieCollection
629     }
630 
631 }

 

2.簡潔模擬HTTP請求

 1  /// <summary>
 2         /// 簡潔模擬HTTP請求
 3         /// </summary>
 4         /// <param name="requestUrl">請求的Url地址</param>
 5         /// <param name="requestMethod">目前支持字符串格式的get或post</param>
 6         /// <param name="postData">post請求時提供如下格式數據:a=1&b=2&c=3...</param>
 7         /// <param name="requestCookie">如果需要cookie請帶上,格式:a=1;b=2;c=3...</param>
 8         /// <param name="requestReferer">來源頁面地址</param>
 9         /// <returns></returns>
10         public HttpResult GetHtml(string requestUrl, string requestMethod = "get", string postData = "", string requestCookie = "", string requestReferer = "")
11         {
12             string requestContentType = requestMethod == "get" ? "text/html" : "application/x-www-form-urlencoded";
13             HttpItem item = new HttpItem()
14             {
15                 URL = requestUrl,//URL     必需項    
16                 Method = requestMethod,//URL     可選項 默認為Get   
17                 IsToLower = false,//得到的HTML代碼是否轉成小寫     可選項默認轉小寫   
18                 Cookie = requestCookie,//字符串Cookie     可選項   
19                 Referer = requestReferer,//來源URL     可選項   
20                 Postdata = postData,//Post數據     可選項GET時不需要寫   
21                 Timeout = 100000,//連接超時時間     可選項默認為100000    
22                 ReadWriteTimeout = 30000,//寫入Post數據超時時間     可選項默認為30000   
23                 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
24                 ContentType = requestContentType,//返回類型    可選項有默認值   
25                 Allowautoredirect = false,//是否根據301跳轉     可選項 
26                 ResultType = ResultType.String,
27                 Encoding = Encoding.UTF8,
28                 PostEncoding = Encoding.UTF8
29             };
30             return GetHtml(item);
31         }
32         public byte[] GetImage(string sourceImgUrl)
33         {
34             HttpItem item = new HttpItem()
35             {
36                 URL = sourceImgUrl,//URL     必需項    
37                 Method = "get",//URL     可選項 默認為Get   
38                 IsToLower = false,//得到的HTML代碼是否轉成小寫     可選項默認轉小寫   
39                 Cookie = "",//字符串Cookie     可選項   
40                 Referer = "",//來源URL     可選項   
41                 Postdata = "",//Post數據     可選項GET時不需要寫   
42                 Timeout = 100000,//連接超時時間     可選項默認為100000    
43                 ReadWriteTimeout = 30000,//寫入Post數據超時時間     可選項默認為30000   
44                 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用戶的瀏覽器類型,版本,操作系統 
45                 ResultType = ResultType.Byte
46             };
47             HttpResult result = GetHtml(item);
48             return result.ResultByte;
49         }
50         public byte[] GetAudio(string sourceUrl, string cookie)
51         {
52             HttpItem item = new HttpItem()
53             {
54                 URL = sourceUrl,//URL     必需項    
55                 Method = "get",//URL     可選項 默認為Get   
56                 IsToLower = false,//得到的HTML代碼是否轉成小寫     可選項默認轉小寫   
57                 Cookie = cookie,//字符串Cookie     可選項   
58                 Referer = "",//來源URL     可選項   
59                 Postdata = "",//Post數據     可選項GET時不需要寫   
60                 Timeout = 100000,//連接超時時間     可選項默認為100000    
61                 ReadWriteTimeout = 30000,//寫入Post數據超時時間     可選項默認為30000   
62                 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用戶的瀏覽器類型,版本,操作系統 
63                 ResultType = ResultType.Byte,
64                 ContentType = "audio/mpeg"
65             };
66             HttpResult result = GetHtml(item);
67             return result.ResultByte;
68         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM