![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HTMLHelper 2 { 3 /// <summary> 4 /// 获取CooKie 5 /// /// </summary> 6 /// /// <param name="loginUrl"></param> 7 /// /// <param name="postdata"></param> 8 /// /// <param name="header"></param> 9 /// /// <returns></returns> 10 public static CookieContainer GetCooKie(string loginUrl, HttpHeader header) 11 { 12 HttpWebRequest request = null; 13 HttpWebResponse response = null; 14 try 15 { 16 CookieContainer cc = new CookieContainer(); 17 request = (HttpWebRequest)WebRequest.Create(loginUrl); 18 request.Method = "GET"; 19 request.ContentType = header.contentType; 20 21 22 request.AllowAutoRedirect = false; 23 request.CookieContainer = cc; 24 request.KeepAlive = true; 25 26 //接收响应 27 response = (HttpWebResponse)request.GetResponse(); 28 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); 29 CookieCollection cook = response.Cookies; //Cookie字符串格式 30 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri); 31 return cc; 32 } 33 catch (Exception ex) 34 { 35 throw ex; 36 } 37 } 38 39 /// <summary> 40 /// 获取CooKie 41 /// /// </summary> 42 /// /// <param name="loginUrl"></param> 43 /// /// <param name="postdata"></param> 44 /// /// <param name="header"></param> 45 /// /// <returns></returns> 46 public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header) 47 { 48 HttpWebRequest request = null; 49 HttpWebResponse response = null; 50 try 51 { 52 CookieContainer cc = new CookieContainer(); 53 request = (HttpWebRequest)WebRequest.Create(loginUrl); 54 request.Method = header.method; 55 request.ContentType = header.contentType; 56 byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata); 57 request.ContentLength = postdatabyte.Length; 58 request.AllowAutoRedirect = false; 59 request.CookieContainer = cc; 60 request.KeepAlive = true; 61 //提交请求 62 Stream stream; 63 stream = request.GetRequestStream(); 64 stream.Write(postdatabyte, 0, postdatabyte.Length); 65 stream.Close(); 66 //接收响应 67 response = (HttpWebResponse)request.GetResponse(); 68 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); 69 CookieCollection cook = response.Cookies; //Cookie字符串格式 70 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri); 71 return cc; 72 } 73 catch (Exception ex) 74 { 75 throw ex; 76 } 77 } 78 /// <summary> 79 /// 获取html 80 /// </summary> 81 /// <param name="getUrl"></param> 82 /// <param name="cookieContainer"></param> 83 /// <param name="header"></param> 84 /// <returns></returns> 85 86 public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header) 87 { 88 Thread.Sleep(1000); 89 HttpWebRequest httpWebRequest = null; 90 HttpWebResponse httpWebResponse = null; 91 try 92 { 93 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl); 94 httpWebRequest.CookieContainer = cookieContainer; 95 httpWebRequest.ContentType = header.contentType; 96 httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry; 97 httpWebRequest.Referer = getUrl; 98 httpWebRequest.Accept = header.accept; 99 httpWebRequest.UserAgent = header.userAgent; 100 httpWebRequest.Method = "GET"; 101 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 102 Stream responseStream = httpWebResponse.GetResponseStream(); 103 StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); 104 string html = streamReader.ReadToEnd(); 105 streamReader.Close(); 106 responseStream.Close(); 107 httpWebRequest.Abort(); 108 httpWebResponse.Close(); 109 return html; 110 } 111 catch (Exception e) 112 { 113 if (httpWebRequest != null) httpWebRequest.Abort(); 114 if (httpWebResponse != null) httpWebResponse.Close(); 115 return string.Empty; 116 } 117 } 118 119 /// <summary> 120 /// 获取html 121 /// </summary> 122 /// <param name="getUrl"></param> 123 /// <param name="cookieContainer"></param> 124 /// <param name="header"></param> 125 /// <returns></returns> 126 127 public static string GetHtml(string getUrl,string post, CookieContainer cookieContainer, HttpHeader header,Encoding en) 128 { 129 Thread.Sleep(1000); 130 HttpWebRequest myHttpWebRequest = null; 131 HttpWebResponse httpWebResponse = null; 132 try 133 { 134 byte[] oneData = Encoding.Default.GetBytes(post); 135 Uri uri = new Uri(getUrl); 136 myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL 137 myHttpWebRequest.CookieContainer = cookieContainer;//*发送COOKIE 138 myHttpWebRequest.Method = header.method; 139 myHttpWebRequest.ContentType = header.contentType; 140 myHttpWebRequest.ContentLength = oneData.Length; 141 Stream newMyStream = myHttpWebRequest.GetRequestStream(); 142 newMyStream.Write(oneData, 0, oneData.Length); 143 144 try 145 { 146 httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 147 StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en); 148 149 string str = sr.ReadToEnd(); 150 151 string msg = string.Empty; 152 153 return str; 154 } 155 catch (Exception ex) 156 { 157 return string.Empty; 158 } 159 } 160 catch (Exception e) 161 { 162 if (myHttpWebRequest != null) myHttpWebRequest.Abort(); 163 if (httpWebResponse != null) httpWebResponse.Close(); 164 return string.Empty; 165 } 166 return string.Empty; 167 } 168 169 /// <summary> 170 /// 获取html 171 /// </summary> 172 /// <param name="getUrl"></param> 173 /// <param name="cookieContainer"></param> 174 /// <param name="header"></param> 175 /// <returns></returns> 176 177 public static string GetHtml(string getUrl, string post, CookieContainer cookieContainer,out CookieContainer co, HttpHeader header, Encoding en) 178 { 179 Thread.Sleep(1000); 180 HttpWebRequest myHttpWebRequest = null; 181 HttpWebResponse httpWebResponse = null; 182 co = new CookieContainer(); 183 try 184 { 185 byte[] oneData = Encoding.Default.GetBytes(post); 186 Uri uri = new Uri(getUrl); 187 myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL 188 if (cookieContainer.Count > 0) 189 { 190 myHttpWebRequest.CookieContainer = cookieContainer; 191 } 192 else 193 { 194 myHttpWebRequest.CookieContainer = co; 195 } 196 197 myHttpWebRequest.Method = header.method; 198 myHttpWebRequest.ContentType = header.contentType; 199 myHttpWebRequest.ContentLength = oneData.Length; 200 Stream newMyStream = myHttpWebRequest.GetRequestStream(); 201 newMyStream.Write(oneData, 0, oneData.Length); 202 203 try 204 { 205 httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 206 StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en); 207 208 httpWebResponse.Cookies = myHttpWebRequest.CookieContainer.GetCookies(myHttpWebRequest.RequestUri); 209 CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式 210 string strcrook = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); 211 212 string str = sr.ReadToEnd(); 213 214 string msg = string.Empty; 215 216 return str; 217 } 218 catch (Exception ex) 219 { 220 return string.Empty; 221 } 222 } 223 catch (Exception e) 224 { 225 if (myHttpWebRequest != null) myHttpWebRequest.Abort(); 226 if (httpWebResponse != null) httpWebResponse.Close(); 227 return string.Empty; 228 } 229 return string.Empty; 230 } 231 232 233 234 /// <summary> 235 /// 获取Stream 236 /// </summary> 237 /// <param name="getUrl"></param> 238 /// <param name="cookieContainer"></param> 239 /// <param name="header"></param> 240 /// <returns></returns> 241 242 public static Stream GetStream(string getUrl, CookieContainer cookieContainer, HttpHeader header) 243 { 244 Thread.Sleep(1000); 245 HttpWebRequest httpWebRequest = null; 246 HttpWebResponse httpWebResponse = null; 247 try 248 { 249 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl); 250 httpWebRequest.CookieContainer = cookieContainer; 251 httpWebRequest.ContentType = header.contentType; 252 httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry; 253 httpWebRequest.Referer = header.referer; 254 httpWebRequest.Accept = header.accept; 255 httpWebRequest.UserAgent = header.userAgent; 256 httpWebRequest.Method = "GET"; 257 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 258 Stream responseStream = httpWebResponse.GetResponseStream(); 259 260 return responseStream; 261 } 262 catch (Exception e) 263 { 264 if (httpWebRequest != null) httpWebRequest.Abort(); 265 if (httpWebResponse != null) httpWebResponse.Close(); 266 return null; 267 } 268 } 269 270 public static Stream GetStream(string getUrl, CookieContainer cookieContainer, out CookieContainer co, HttpHeader header) 271 { 272 273 Thread.Sleep(1000); 274 HttpWebRequest httpWebRequest = null; 275 HttpWebResponse httpWebResponse = null; 276 co = new CookieContainer(); 277 try 278 { 279 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl); 280 if (cookieContainer.Count > 0) 281 { 282 httpWebRequest.CookieContainer = cookieContainer; 283 } 284 else 285 { 286 httpWebRequest.CookieContainer = co; 287 } 288 289 httpWebRequest.ContentType = header.contentType; 290 httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry; 291 httpWebRequest.Referer = getUrl; 292 httpWebRequest.Accept = header.accept; 293 httpWebRequest.UserAgent = header.userAgent; 294 httpWebRequest.Method = "GET"; 295 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 296 Stream responseStream = httpWebResponse.GetResponseStream(); 297 298 299 300 httpWebResponse.Cookies = httpWebRequest.CookieContainer.GetCookies(httpWebRequest.RequestUri); 301 CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式 302 string strcrook = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.RequestUri); 303 304 305 return responseStream; 306 } 307 catch (Exception e) 308 { 309 if (httpWebRequest != null) httpWebRequest.Abort(); 310 if (httpWebResponse != null) httpWebResponse.Close(); 311 return null; 312 } 313 } 314 315 } 316 317 318 319 public class HttpHeader 320 { 321 public string contentType { get; set; } 322 public string accept { get; set; } 323 public string userAgent { get; set; } 324 public string method { get; set; } 325 public int maxTry { get; set; } 326 public string referer { get; set; } 327 }