我是靠谱客的博主 甜美茉莉,最近开发中收集的这篇文章主要介绍模拟Http请求,Ajax和获取Html(全),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 需要两个辅助类库 HtmlAgilityPack.dll和Newtonsoft.Json.dll

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using HtmlAgilityPack;
  5 using Newtonsoft.Json;
  6 using System.IO;
  7 using System.Net;
  8 using System.Xml;
  9 using System.Drawing;
 10 using System.Xml.Linq;
 11 namespace Web.Http
 12 {
 13     public delegate void WebException(Uri url,Exception e);
 14     public delegate void HeaderAdd(WebHeaderCollection collection);
 15     public class HttpQuery
 16     {
 17         #region 常量
 18         private const string HTTP_CLIENT_USER_ANGENT = "Windows Form:[Just Test ]";
 19         private const string SESSION_SET = "Set-Cookie";
 20         private const string IMAGE_ACCEPT = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5";
 21         private const string JSON_ACCEPT = "application/json, text/javascript, */*; q=0.01";
 22         private const string JSON_CONTENTTYPE = "application/json; charset=utf-8";
 23         private const string HEADER_RVT = "X-rvt";
 24         private const string HEADER_XMLHTTPREQUEST = "X-Requested-With";
 25         private const string HEADER_XMLHTTPREQUEST_VALUE = "XMLHttpRequest";
 26         private const string DEFAULT_JSON_NAME = "DefaultJSONNode";
 27         public const string METHOD_POST = "post";
 28         public const string METHOD_GET = "get";
 29         public const string METHOD_HEAD = "head";
 30         private const string DATE = "Date";
 31         #endregion
 32         #region 私有变量
 33         private volatile CookieContainer cookies=new CookieContainer();
 34         private bool userCookies = true;
 35         private DateTime nTime;
 36         private HeaderAdd headerAdd;
 37         #endregion
 38         #region 属性
 39         /// <summary>
 40         /// 根据响应结果返回的服务器时间
 41         /// </summary>
 42         public DateTime NowTime
 43         {
 44             get { return nTime; }
 45         }
 46         /// <summary>
 47         /// 获取或设置如何处理请求的标头的委托
 48         /// </summary>
 49         public HeaderAdd HeaderAdd
 50         {
 51             get { return headerAdd; }
 52             set { headerAdd = value; }
 53         }
 54         /// <summary>
 55         /// 获取或设置是否使用Cookies,默认为true
 56         /// </summary>
 57         public bool UserCookies
 58         {
 59             get { return userCookies; }
 60             set {
 61                 userCookies = value;
 62                 if (!userCookies)
 63                 {
 64                     cookies = null;
 65                 }
 66             }
 67         }
 68         #endregion
 69         #region 事件
 70         public event WebException GetException;
 71         #endregion
 72         #region 构造函数
 73         public HttpQuery()
 74         {
 75            
 76         }
 77         #endregion
 78         #region 基方法
 79         /// <summary>
 80         /// 创建Http请求
 81         /// </summary>
 82         /// <param name="url">URL</param>
 83         /// <param name="accept">Header-ACCEPT</param>
 84         /// <param name="method">POST或GET或HEAD</param>
 85         /// <param name="referURL">Header-Refer</param>
 86         /// <returns></returns>
 87         private HttpWebRequest CreateRequest(string url, string accept, string method,
 88             string referURL)
 89         {
 90             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
 91             request.CookieContainer = cookies;
 92             request.KeepAlive = true;
 93             if (referURL != null)
 94             {
 95                 request.Referer = referURL;
 96             }
 97             if (accept != null)
 98             {
 99                 request.Accept = accept;
100             }
101             switch (method.ToLower())
102             {
103                 case METHOD_POST:
104                     request.Method = WebRequestMethods.Http.Post;
105                     break;
106                 case METHOD_GET:
107                     request.Method = WebRequestMethods.Http.Get;
108                     break;
109                 case METHOD_HEAD:
110                     request.Method = WebRequestMethods.Http.Head;
111                     break;
112                 default:
113                     break;
114             }
115             if (HeaderAdd != null)
116             {
117                 HeaderAdd.Invoke(request.Headers);
118             }
119             request.UserAgent = HTTP_CLIENT_USER_ANGENT;
120             request.AllowAutoRedirect = true;
121             request.ContentLength = 0;
122             return request;
123         }
124         /// <summary>
125         /// 设置提交请求时的响应
126         /// </summary>
127         /// <param name="data"></param>
128         /// <param name="request"></param>
129         private void SetRequestData(byte[] data, HttpWebRequest request)
130         {
131             if (data != null)
132             {
133                 byte[] inputData = data;
134                 request.ContentLength = inputData.Length;
135                 Stream stream = request.GetRequestStream();
136                 stream.Write(inputData, 0, inputData.Length);
137             }
138         }
139         private bool GetWebData(HttpWebRequest request, out string data)
140         {
141             byte[] bData = null;
142             if (GetWebData(request, out bData))
143             {
144                 data=Encoding.UTF8.GetString(bData);
145                 return true;
146             }
147             else
148             {
149                 data = null;
150                 return false;
151             }
152         }
153         private bool GetWebData(HttpWebRequest request, out byte[] data)
154         {
155             HttpWebResponse response = null;
156             if (GetResponse(request, out response))
157             {
158                 Stream stream = null;
159                 if (GetResponseStream(response, request.RequestUri, out stream))
160                 {
161                     long len = response.ContentLength;
162                     if (len != 0)
163                     {
164                         data = new byte[(int)len];
165                         stream.Read(data, 0, data.Length);
166                         return true;
167                     }
168                     else
169                     {
170                         data = null;
171                         return false;
172                     }
173                 }
174                 else
175                 {
176                     data = null;
177                     return false;
178                 }
179             }
180             else
181             {
182                 data = null;
183                 return false;
184             }
185         }
186         private bool GetHtmlDocument(string url, string method, string referURL, int waitTimeout,byte[] requestData,out HtmlDocument document)
187         {
188             HttpWebRequest request = NormalRequestCreate(url, method, referURL, waitTimeout, requestData);
189             string htmlData = null;
190             if (GetWebData(request, out htmlData))
191             {
192                 document = new HtmlDocument();
193                 document.LoadHtml(htmlData);
194                 return true;
195             }
196             else
197             {
198                 document = null;
199                 return false;
200             }
201         }
202         private bool Ajax(string url, string method, string referURL, int waitTimeout, byte[] requestData, out string returnData)
203         {
204             HttpWebRequest request = CreateAjaxRequest(url, method, referURL, waitTimeout, requestData);
205             if (GetWebData(request, out returnData))
206             {
207                 XDocument xDoc = JsonConvert.DeserializeXNode(returnData);
208                 returnData = xDoc.ToString();
209                 return true;
210             }
211             else
212             {
213                 return false;
214             }
215         }
216         private HttpWebRequest CreateAjaxRequest(string url, string method, string referURL,int waitTimeout, byte[] requestData)
217         {
218             HttpWebRequest request = NormalRequestCreate(url, method, referURL, waitTimeout,null);
219             request.Accept = JSON_ACCEPT;
220             request.ContentType = JSON_CONTENTTYPE;
221             request.Headers.Add(HEADER_XMLHTTPREQUEST, HEADER_XMLHTTPREQUEST_VALUE);
222             if (requestData != null)
223             {
224                 SetRequestData(requestData, request);
225             }
226             return request;
227         }
228         private HttpWebRequest NormalRequestCreate(string url, string method, string referURL, int waitTimeout, byte[] requestData)
229         {
230             HttpWebRequest request = CreateRequest(url, null, method, referURL);
231             if (waitTimeout != -1)
232             {
233                 request.Timeout = waitTimeout;
234             }
235             if (requestData != null)
236             {
237                 SetRequestData(requestData, request);
238             }
239             return request;
240         }
241         private bool GetResponseStream(HttpWebResponse response,Uri requestURL,out Stream stream)
242         {
243             try
244             {
245                 stream = response.GetResponseStream();
246                 return true;
247             }
248             catch (Exception e)
249             {
250                 stream = null;
251                 if (GetException != null)
252                 {
253                     GetException(requestURL, e);
254                 }
255                 return false;
256             }
257         }
258         private XmlDocument GetJsonForDocument(string outData,string name)
259         {
260             return JsonConvert.DeserializeXmlNode(outData, name);
261         }
262         /// <summary>
263         /// 获取响应
264         /// </summary>
265         /// <param name="request">Http请求</param>
266         /// <param name="response">返回Http响应</param>
267         /// <returns>获取响应是否成功</returns>
268         private bool GetResponse(HttpWebRequest request, out HttpWebResponse response)
269         {
270             try
271             {
272                 response = (HttpWebResponse)request.GetResponse();
273                 if (userCookies)
274                 {
275                     string sessionCode = response.GetResponseHeader(SESSION_SET);
276                     if (sessionCode != null)
277                     {
278                         cookies.SetCookies(response.ResponseUri, sessionCode);
279                     }
280                 }
281                 string date=response.GetResponseHeader(DATE);
282                 if (date!=null)
283                 {
284                     nTime = Convert.ToDateTime(date).ToUniversalTime();
285                 }
286                 return true;
287             }
288             catch (Exception e)
289             {
290                 response = null;
291                 if (GetException != null)
292                 {
293                     GetException(request.RequestUri, e);
294                 }
295                 return false;
296             }
297         }
298         #endregion
299         #region HTML操作公开方法
300         /// <summary>
301         /// 获取HTML文档
302         /// </summary>
303         /// <param name="url"></param>
304         /// <param name="method"></param>
305         /// <param name="referURL"></param>
306         /// <param name="waitTimeout"></param>
307         /// <param name="requestData"></param>
308         /// <returns></returns>
309         public HtmlDocument GetHtmlDocument(string url, string method, string referURL, int waitTimeout,byte[] requestData)
310         { 
311             HtmlDocument htmlDocument=null;
312             if (GetHtmlDocument(url, method, referURL, waitTimeout, requestData, out htmlDocument))
313             {
314                 return htmlDocument;
315             }
316             else
317             {
318                 return null;
319             }
320         }
321         /// <summary>
322         /// 获取Html文档,默认不超时
323         /// </summary>
324         /// <param name="url"></param>
325         /// <param name="method"></param>
326         /// <param name="referURL"></param>
327         /// <param name="requestData"></param>
328         /// <returns></returns>
329         public HtmlDocument GetHtmlDocument(string url, string method, string referURL, byte[] requestData)
330         {
331             return GetHtmlDocument(url, method, referURL, -1, requestData);
332         }
333         #endregion
334         #region Ajax公开方法
335         /// <summary>
336         /// 模拟Ajax返回字符串
337         /// </summary>
338         /// <param name="url">URL</param>
339         /// <param name="method">Method</param>
340         /// <param name="referURL">Refer</param>
341         /// <param name="waitTimeout">请求超时时间</param>
342         /// <param name="requestData">提交的参数</param>
343         /// <returns></returns>
344         public string Ajax_String(string url, string method, string referURL, int waitTimeout, byte[] requestData)
345         {
346             string outData = null;
347             Ajax(url, method, referURL, waitTimeout, requestData, out outData);
348             return outData;
349         }
350         /// <summary>
351         /// 模拟Ajax,不带请求数据
352         /// </summary>
353         /// <param name="url">URL</param>
354         /// <param name="method">Method</param>
355         /// <param name="referURL">Refer</param>
356         /// <param name="waitTimeout">请求超时时间</param>
357         /// <returns></returns>
358         public string Ajax_String(string url, string method, string referURL, int waitTimeout)
359         {
360             return Ajax_String(url, method, referURL, waitTimeout, null);
361         }
362         /// <summary>
363         /// 模拟Ajax,不设置超时时间
364         /// </summary>
365         /// <param name="url">URL</param>
366         /// <param name="method">Method</param>
367         /// <param name="referURL">Refer</param>
368         /// <param name="requestData">请求超时时间</param>
369         /// <returns></returns>
370         public string Ajax_String(string url, string method, string referURL,byte[] requestData)
371         {
372             return Ajax_String(url, method, referURL, -1, requestData);
373         }
374         /// <summary>
375         /// 模拟Ajax,即不设置超时时间,也不请求数据
376         /// </summary>
377         /// <param name="url">URL</param>
378         /// <param name="method">Method</param>
379         /// <param name="referURL">Refer</param>
380         /// <returns></returns>
381         public string Ajax_String(string url, string method, string referURL)
382         {
383             return Ajax_String(url, method, referURL,null);
384         }
385         #endregion
386         #region Ajax_Json公开方法
387         /// <summary>
388         /// 模拟Ajax返回Json的Xml化数据
389         /// </summary>
390         /// <param name="url">URL</param>
391         /// <param name="name">根节点名称</param>
392         /// <param name="method">Method</param>
393         /// <param name="referURL">Refer</param>
394         /// <param name="waitTimeout">超时时间</param>
395         /// <param name="requestData">请求数据</param>
396         /// <returns></returns>
397         public XmlDocument JSONForXml(string url, string name, string method, string referURL, int waitTimeout, byte[] requestData)
398         {
399             string outData = Ajax_String(url, method, referURL, waitTimeout, requestData);
400             if (outData != null)
401             {
402                 return GetJsonForDocument(outData, name);
403             }
404             else
405             {
406                 return null;
407             }
408         }
409         /// <summary>
410         /// 模拟Ajax返回Json的Xml化数据,不设置超时时间
411         /// </summary>
412         /// <param name="url">URL</param>
413         /// <param name="name">根节点名称</param>
414         /// <param name="method">Method</param>
415         /// <param name="referURL">Refer</param>
416         /// <param name="requestData">请求数据</param>
417         /// <returns></returns>
418         public XmlDocument JSONForXml(string url, string name, string method, string referURL, byte[] requestData)
419         {
420             return JSONForXml(url, name, method, referURL, -1, requestData);
421         }
422         /// <summary>
423         /// 模拟Ajax返回Json的Xml化数据,即不设置超时时间,也不设置请求数据
424         /// </summary>
425         /// <param name="url">URL</param>
426         /// <param name="name">根节点名称</param>
427         /// <param name="method">Method</param>
428         /// <param name="referURL">Refer</param>
429         /// <param name="waitTimeout">超时时间</param>
430         /// <returns></returns>
431         public XmlDocument JSONForXml(string url, string name, string method, string referURL)
432         {
433             return JSONForXml(url, name, method, referURL, -1, null);
434         }
435         /// <summary>
436         /// 模拟Ajax返回Json的Xml化数据,不设置请求数据
437         /// </summary>
438         /// <param name="url">URL</param>
439         /// <param name="name">根节点名称</param>
440         /// <param name="method">Method</param>
441         /// <param name="referURL">Refer</param>
442         ///  <param name="waitTimeout">超时时间</param>
443         /// <returns></returns>
444         public XmlDocument JSONForXml(string url, string name, string method, string referURL, int waitTimeout)
445         {
446             return JSONForXml(url, name, method, referURL,waitTimeout, null);
447         }
448 
449         /// <summary>
450         /// 模拟Ajax返回Json的Xml化数据,默认根节点名称
451         /// </summary>
452         /// <param name="url">URL</param>
453         /// <param name="method">Method</param>
454         /// <param name="referURL">Refer</param>
455         /// <param name="waitTimeout">超时时间</param>
456         /// <param name="requestData">请求数据</param>
457         /// <returns></returns>
458         public XmlDocument JSONForXml(string url, string method, string referURL, int waitTimeout, byte[] requestData)
459         {
460             return JSONForXml(url,DEFAULT_JSON_NAME,method, referURL, waitTimeout, requestData);
461         }
462         /// <summary>
463         /// 模拟Ajax返回Json的Xml化数据,默认根节点名称,不设置超时时间
464         /// </summary>
465         /// <param name="url">URL</param>
466         /// <param name="method">Method</param>
467         /// <param name="referURL">Refer</param>
468         /// <param name="requestData">请求数据</param>
469         /// <returns></returns>
470         public XmlDocument JSONForXml(string url, string method, string referURL, byte[] requestData)
471         {
472             return JSONForXml(url,method, referURL, -1, requestData);
473         }
474         /// <summary>
475         /// 模拟Ajax返回Json的Xml化数据,默认根节点名称,即不设置超时时间,也不设置请求数据
476         /// </summary>
477         /// <param name="url">URL</param>
478         /// <param name="method">Method</param>
479         /// <param name="referURL">Refer</param>
480         /// <param name="waitTimeout">超时时间</param>
481         /// <returns></returns>
482         public XmlDocument JSONForXml(string url, string method, string referURL)
483         {
484             return JSONForXml(url, method, referURL, -1, null);
485         }
486         /// <summary>
487         /// 模拟Ajax返回Json的Xml化数据,默认根节点名称,不设置请求数据
488         /// </summary>
489         /// <param name="url">URL</param>
490         /// <param name="method">Method</param>
491         /// <param name="referURL">Refer</param>
492         ///  <param name="waitTimeout">超时时间</param>
493         /// <returns></returns>
494         public XmlDocument JSONForXml(string url, string method, string referURL, int waitTimeout)
495         {
496             return JSONForXml(url,method, referURL, waitTimeout, null);
497         }
498         #endregion
499         #region 获取图片公开方法
500         /// <summary>
501         /// 获取Web上的图片
502         /// </summary>
503         /// <param name="url">URL</param>
504         /// <param name="method">Method</param>
505         /// <param name="referURL">Refer</param>
506         /// <param name="image">返回的图片</param>
507         /// <returns>是否获取成功</returns>
508         public bool GetImage(string url,string method,string referURL,out Image image)
509         {
510             HttpWebRequest request = NormalRequestCreate(url, method, referURL, -1, null);
511             request.Accept = IMAGE_ACCEPT;
512             byte[] imgData=null;
513             if (GetWebData(request, out imgData))
514             {
515                 MemoryStream mStream = new MemoryStream(imgData);
516                 image = new Bitmap(mStream);
517                 return true;
518             }
519             else
520             {
521                 image = null;
522                 return false;
523             }
524         }
525         #endregion
526     }
527 }
View Code

 

转载于:https://www.cnblogs.com/zmx-mk/p/HttpWorker.html

最后

以上就是甜美茉莉为你收集整理的模拟Http请求,Ajax和获取Html(全)的全部内容,希望文章能够帮你解决模拟Http请求,Ajax和获取Html(全)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部