[c#] HttpWebRequest简单实用封装应用类

   很多时候我们要用HttpWebRequest组件模拟http协议发送web请求,封装一个请求类看起来就很有必要了。要很好的封装一个这样的类,要考虑很多因素,比如通用性和异常处理,甚至还可以是继承性。当然,我下面要说的是一个静态类,继承什么的就算了,简单封装一下能用就行,呵呵,太懒了。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Net;
  5 using System.IO;
  6 
  7 namespace Uu102
8 { 9 /// <summary> 10 ///http协议模拟请求类 11 ///主要考虑请求后的具体结果 12 ///此类出自 uu102.com,请尊重原作者,转载请加上此链接 13 /// </summary> 14 class HttpHelper 15 { 16 /// <summary> 17 /// 请求并发限制数目 18 /// </summary> 19 private static int DefaultConnectionLimit=1; 20 private const string Accept = "*/*"; 21 private const string UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; 22 private const string ContentType = "application/x-www-form-urlencoded"; 23 24 /// <summary> 25 /// 发送资源请求。返回请求到的响应文本 26 /// 之所以看到这么多形参,只是本人的水平很菜的体现^.^ 27 /// 注意,这个类并没有处理https加密请求 28 /// </summary> 29 /// <param name="url">发送请求的url地址</param> 30 /// <param name="postString"></param> 31 /// <param name="IsPost"></param> 32 /// <param name="cookieContainer"></param> 33 /// <param name="referer"></param> 34 /// <param name="encoding"></param> 35 /// <returns></returns> 36 public static string GetHtml(string url, string postString, bool IsPost, CookieContainer cookieContainer, string referer, Encoding encoding) 37 { 38 string html = string.Empty; 39 ServicePointManager.Expect100Continue = false; 40 ServicePointManager.DefaultConnectionLimit = DefaultConnectionLimit;//设置并发连接数限制上额 41 DefaultConnectionLimit++; 42 if (string.IsNullOrEmpty(postString)) IsPost = false; 43 HttpWebRequest httpWebRequest = null; 44 45 HttpWebResponse httpWebResponse = null; 46 try 47 { 48 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);//创建连接请求 49 httpWebRequest.Method = IsPost ? "POST" : "GET"; 50 if (cookieContainer != null) httpWebRequest.CookieContainer = cookieContainer; 51 httpWebRequest.AllowAutoRedirect = true;//【注意】这里有个时候在特殊情况下要设置为否,否则会造成cookie丢失 52 httpWebRequest.ContentType = ContentType; 53 httpWebRequest.Accept = Accept; 54 httpWebRequest.UserAgent = UserAgent; 55 if (!string.IsNullOrEmpty(referer)) httpWebRequest.Referer = referer; 56 if (IsPost) //如果是Post递交数据,则写入传的字符串数据 57 { 58 byte[] byteRequest = Encoding.Default.GetBytes(postString); 59 httpWebRequest.ContentLength = byteRequest.Length; 60 Stream stream = httpWebRequest.GetRequestStream(); 61 stream.Write(byteRequest, 0, byteRequest.Length); 62 stream.Close(); 63 } 64 65 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//开始获取响应流 66 Stream responseStream = httpWebResponse.GetResponseStream(); 67 StreamReader streamReader = new StreamReader(responseStream, encoding); 68 html = streamReader.ReadToEnd();//注意这里是直接将所有的字节从头读到尾,也可以一行一行的控制,节省时间 69 streamReader.Close(); 70 responseStream.Close(); 71 httpWebRequest.Abort(); 72 73 foreach (Cookie cookie in httpWebResponse.Cookies) //获取cookie 74 { 75 cookieContainer.Add(cookie); 76 } 77 78 httpWebResponse.Close(); 79 //到这里为止,所有的对象都要释放掉,以免内存像滚雪球一样 80 return html; 81 } 82 catch 83 { 84 DefaultConnectionLimit--; 85 //我这里就没做任何处理了,这里最好还是处理一下 86 return null; 87 } 88 89 } 90 #region 重载方法 91 public static string GetHtml(string url, string postString, CookieContainer cookieContainer, string referer) 92 { 93 return GetHtml(url, postString, true, cookieContainer, referer, Encoding.UTF8); 94 } 95 public static string GetHtml(string url, string postString, CookieContainer cookieContainer) 96 { 97 return GetHtml(url, postString, true, cookieContainer, url, Encoding.UTF8); 98 } 99 public static string GetHtml(string url, CookieContainer cookieContainer, string referer) 100 { 101 return GetHtml(url, "", false, cookieContainer, referer, Encoding.UTF8); 102 } 103 public static string GetHtml(string url, CookieContainer cookieContainer) 104 { 105 return GetHtml(url, "", false, cookieContainer, url, Encoding.UTF8); 106 } 107 public static string GetHtml(string url) 108 { 109 return GetHtml(url, "", true, null, url, Encoding.UTF8); 110 } 111 #endregion 112 /// <summary> 113 /// 这个方法主要用来获取非文本的html响应正文 114 /// </summary> 115 /// <param name="url"></param> 116 /// <param name="cookieContainer"></param> 117 /// <returns></returns> 118 public static Stream GetStream(string url,CookieContainer cookieContainer) 119 { 120 HttpWebRequest httpWebRequest = null; 121 HttpWebResponse httpWebResponse = null; 122 123 try 124 { 125 126 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 127 httpWebRequest.CookieContainer = cookieContainer; 128 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 129 Stream responseStream = httpWebResponse.GetResponseStream(); 130 return responseStream; 131 } 132 catch 133 { 134 if (httpWebRequest != null) 135 { 136 httpWebRequest.Abort(); 137 } if (httpWebResponse != null) 138 { 139 httpWebResponse.Close(); 140 } 141 return null; 142 } 143 } 144 145 } 146 }

 

代码太简单解释都免了吧?要强调的一点,上面的封装类还没有处理https这样的加密连接,以及返回的status和异常,如果是自己调试,最好是将try这个功能注释掉,免得看了半天都不知道哪出了问题。
以上的功能虽然能适合绝大部分请求,但是仍有一些请客不适合,比如当模拟上传文件等等的情况就不适用。

下次我们就聊聊加密请求,以及模拟上传文件的方法吧!

如果您觉得本文对您有帮助,就请加关注本文博客吧!

 

posted on 2012-09-06 13:11  凌晨的搜索者  阅读(5143)  评论(3编辑  收藏  举报

导航