麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ahjesus HttpQuery

2019-11-17 01:56:10
字體:
來源:轉載
供稿:網友

ahjesus HttpQuery

  1     /// <summary>    2     /// 有關HTTP請求的輔助類    3     /// </summary>    4     public class HttpQuery  5     {  6         PRivate static readonly string DefaultUserAgent =  7             "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";  8   9         public static void Get(string url, object data, Action<string> callback) 10         { 11             IDictionary<string, string> parameters = Getparameters(data); 12  13             if (!(parameters == null || parameters.Count == 0)) 14             { 15                 url += "?"; 16                 foreach (var item in parameters) 17                 { 18                     url += item.Key + "=" + item.Value + "&"; 19                 } 20             } 21             CreateGetHttpResponse(url, null, null, null, callback); 22         } 23  24         public static void Post(string url, object data, Action<string> callback) 25         { 26             IDictionary<string, string> parameters = Getparameters(data); 27  28             CreatePostHttpResponse(url, parameters, null, null, Encoding.UTF8, null, callback); 29         } 30  31         public static void Post(string url, string data, Action<string> callback) 32         { 33             CreatePostHttpResponse(url, data, null, null, Encoding.UTF8, null, callback); 34         } 35  36         private static IDictionary<string, string> Getparameters(object data) 37         { 38             if (data == null) 39             { 40                 return new Dictionary<string, string>(); 41             } 42             IDictionary<string, string> parameters = new Dictionary<string, string>(); 43  44             Type type = data.GetType(); 45             PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); 46             foreach (PropertyInfo p in props) 47             { 48                 parameters.Add(p.Name, p.GetValue(data).ToString()); 49             } 50  51             return parameters; 52         } 53  54         /// <summary>   55         /// 創建GET方式的HTTP請求   56         /// </summary>   57         /// <param name="url">請求的URL</param>   58         /// <param name="timeout">請求的超時時間</param>   59         /// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param>   60         /// <param name="cookies">隨同HTTP請求發送的Cookie信息,如果不需要身份驗證可以為空</param>   61         /// <returns></returns>   62         private static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, 63             CookieCollection cookies, Action<string> callback, string encoding = "utf-8") 64         { 65             if (string.IsNullOrEmpty(url)) 66             { 67                 return null; 68                 //throw new ArgumentNullException("url"); 69             } 70             try 71             { 72                 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 73                 request.Method = "GET"; 74                 request.UserAgent = DefaultUserAgent; 75                 if (!string.IsNullOrEmpty(userAgent)) 76                 { 77                     request.UserAgent = userAgent; 78                 } 79                 if (timeout.HasValue) 80                 { 81                     request.Timeout = timeout.Value; 82                 } 83                 if (cookies != null) 84                 { 85                     request.CookieContainer = new CookieContainer(); 86                     request.CookieContainer.Add(cookies); 87                 } 88  89                 HttpWebResponse httpWebResponse = request.GetResponse() as HttpWebResponse; 90  91                 StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), 92                     System.Text.Encoding.GetEncoding(encoding)); 93  94                 string html = ""; 95                 //獲取請求到的數據 96                 html = reader.ReadToEnd(); 97                 //關閉 98                 httpWebResponse.Close(); 99                 reader.Close();100 101                 Regex regex = new Regex("charset=(?<code>//w+)/"");102                 Match match = regex.Match(html);103                 string code = match.Groups["code"].Value;104                 if (!string.IsNullOrWhiteSpace(code) && code.ToLower() != encoding.ToLower())105                 {106                     return CreateGetHttpResponse(url, timeout, userAgent, cookies, callback, code);107                 }108                 else109                 {110                     callback(html);111                     return httpWebResponse;112                 }113             }114             catch115             {116                 callback(null);117             }118             return null;119         }120 121         /// <summary>  122         /// 創建POST方式的HTTP請求  123         /// </summary>  124         /// <param name="url">請求的URL</param>  125         /// <param name="parameters">隨同請求POST的參數名稱及參數值字典</param>  126         /// <param name="timeout">請求的超時時間</param>  127         /// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param>  128         /// <param name="requestEncoding">發送HTTP請求時所用的編碼</param>  129         /// <param name="cookies">隨同HTTP請求發送的Cookie信息,如果不需要身份驗證可以為空</param>  130         /// <returns></returns>  131         private static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,132             int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies, Action<string> callback)133         {134             if (string.IsNullOrEmpty(url))135             {136                 throw new ArgumentNullException("url");137             }138             if (requestEncoding == null)139             {140                 throw new ArgumentNullException("requestEncoding");141             }142             HttpWebRequest request = null;143             try144             {145                 //如果是發送HTTPS請求  146                 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))147                 {148                     ServicePointManager.ServerCertificateValidationCallback =149                         new RemoteCertificateValidationCallback(CheckValidationResult);150                     request = WebRequest.Create(url) as HttpWebRequest;151                     request.ProtocolVersion = HttpVersion.Version10;152                 }153                 else154                 {155                     request = WebRequest.Create(url) as HttpWebRequest;156                 }157                 request.Method = "POST";158                 request.ContentType = "application/x-www-form-urlencoded";159 160                 if (!string.IsNullOrEmpty(userAgent))161                 {162                     request.UserAgent = userAgent;163                 }164                 else165                 {166                     request.UserAgent = DefaultUserAgent;167                 }168 169                 if (timeout.HasValue)170                 {171                     request.Timeout = timeout.Value;172                 }173                 if (cookies != null)174                 {175                     request.CookieContainer = new CookieContainer();176                     request.CookieContainer.Add(cookies);177                 }178                 //如果需要POST數據  179                 if (!(parameters == null || parameters.Count == 0))180                 {181                     StringBuilder buffer = new StringBuilder();182                     int i = 0;183
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品av久久久久久网址 | 亚洲午夜一区二区三区 | 在线成人免费网站 | 精品中文字幕久久久久四十五十骆 | 日韩黄色免费电影 | 欧美成人午夜精品久久久 | 成人在线观看免费高清 | 亚洲成人精品久久 | 国产亚洲精品一区二区三区 | 日本视频网 | 最新中文在线视频 | 日韩精品免费一区二区三区 | 国产永久免费观看 | 婷婷中文字幕一区二区三区 | 免费在线观看毛片视频 | 操碰网 | 在线 日本 制服 中文 欧美 | 一级免费看片 | 青青草成人影视 | 91精品成人福利在线播放 | 欧美一极视频 | 欧美精品免费一区二区三区 | 亚洲精品动漫在线观看 | 特级a欧美做爰片毛片 | 日本一道aⅴ不卡免费播放 视屏一区 | 精品无吗乱吗av国产爱色 | 久色伊人 | 中午日产幕无线码1区 | 欧美成人黄色小视频 | 欧美一级二级毛片视频 | 欧美片a | 一级电影在线观看 | 国产1区2区在线观看 | 亚洲一区 国产精品 | 久久爽精品区穿丝袜 | 一色桃子av大全在线播放 | 欧美一级淫片a免费播放口 91九色蝌蚪国产 | 在线视频1区 | 日本中文一级片 | 黄色免费在线电影 | 人禽l交免费视频 |