├── HttpHelper.cs
└── README.md
/HttpHelper.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Created by SharpDevelop.
3 | * User: RedXu
4 | * Date: 2015-04-16
5 | * Time: 13:58
6 | *
7 | */
8 | using System;
9 | using System.Net;
10 | using System.IO;
11 | using System.Text;
12 | using System.Collections.Generic;
13 | using System.Diagnostics;
14 | using System.Net.Security;
15 | using System.Security.Cryptography.X509Certificates;
16 |
17 | namespace RedXuCSharpClass
18 | {
19 | ///
20 | /// Http操作类.
21 | ///
22 | public class HttpHelper
23 | {
24 | private const int ConnectionLimit = 100;
25 | //编码
26 | private Encoding _encoding = Encoding.Default;
27 | //浏览器类型
28 | private string[] _useragents = new string[]{
29 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
30 | "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)",
31 | "Mozilla/5.0 (Windows NT 6.1; rv:36.0) Gecko/20100101 Firefox/36.0",
32 | "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0"
33 | };
34 |
35 | private String _useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";
36 | //接受类型
37 | private String _accept = "text/html, application/xhtml+xml, application/xml, */*";
38 | //超时时间
39 | private int _timeout = 30*1000;
40 | //类型
41 | private string _contenttype = "application/x-www-form-urlencoded";
42 | //cookies
43 | private String _cookies = "";
44 | //cookies
45 | private CookieCollection _cookiecollection;
46 | //custom heads
47 | private Dictionary _headers = new Dictionary();
48 |
49 | public HttpHelper()
50 | {
51 | _headers.Clear();
52 | //随机一个useragent
53 | _useragent = _useragents[new Random().Next(0,_useragents.Length)];
54 | //解决性能问题?
55 | ServicePointManager.DefaultConnectionLimit = ConnectionLimit;
56 | }
57 |
58 | public void InitCookie()
59 | {
60 | _cookies = "";
61 | _cookiecollection = null;
62 | _headers.Clear();
63 | }
64 |
65 | ///
66 | /// 设置当前编码
67 | ///
68 | ///
69 | public void SetEncoding(Encoding en)
70 | {
71 | _encoding = en;
72 | }
73 |
74 | ///
75 | /// 设置UserAgent
76 | ///
77 | ///
78 | public void SetUserAgent(String ua)
79 | {
80 | _useragent = ua;
81 | }
82 |
83 | public void RandUserAgent()
84 | {
85 | _useragent = _useragents[new Random().Next(0,_useragents.Length)];
86 | }
87 |
88 | public void SetCookiesString(string c)
89 | {
90 | _cookies = c;
91 | }
92 |
93 | ///
94 | /// 设置超时时间
95 | ///
96 | ///
97 | public void SetTimeOut(int msec)
98 | {
99 | _timeout = msec;
100 | }
101 |
102 | public void SetContentType(String type)
103 | {
104 | _contenttype = type;
105 | }
106 |
107 | public void SetAccept(String accept)
108 | {
109 | _accept = accept;
110 | }
111 |
112 | ///
113 | /// 添加自定义头
114 | ///
115 | ///
116 | ///
117 | public void AddHeader(String key,String ctx)
118 | {
119 | //_headers.Add(key,ctx);
120 | _headers[key] = ctx;
121 | }
122 |
123 | ///
124 | /// 清空自定义头
125 | ///
126 | public void ClearHeader()
127 | {
128 | _headers.Clear();
129 | }
130 |
131 | ///
132 | /// 获取HTTP返回的内容
133 | ///
134 | ///
135 | ///
136 | private String GetStringFromResponse(HttpWebResponse response)
137 | {
138 | String html = "";
139 | try
140 | {
141 | Stream stream = response.GetResponseStream();
142 | StreamReader sr = new StreamReader(stream,_encoding);
143 | html = sr.ReadToEnd();
144 |
145 | sr.Close();
146 | stream.Close();
147 | }
148 | catch(Exception e)
149 | {
150 | Trace.WriteLine("GetStringFromResponse Error: " + e.Message);
151 | }
152 |
153 | return html;
154 | }
155 |
156 | ///
157 | /// 检测证书
158 | ///
159 | ///
160 | ///
161 | ///
162 | ///
163 | ///
164 | private bool CheckCertificate(object sender,X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
165 | {
166 | return true;
167 | }
168 |
169 | ///
170 | /// 发送GET请求
171 | ///
172 | ///
173 | ///
174 | public String HttpGet(String url)
175 | {
176 | return HttpGet(url,url);
177 | }
178 |
179 |
180 | ///
181 | /// 发送GET请求
182 | ///
183 | ///
184 | ///
185 | ///
186 | public String HttpGet(String url,String refer)
187 | {
188 | String html;
189 | try
190 | {
191 | ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckCertificate);
192 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
193 | request.UserAgent = _useragent;
194 | request.Timeout = _timeout;
195 | request.ContentType = _contenttype;
196 | request.Accept = _accept;
197 | request.Method = "GET";
198 | request.Referer = refer;
199 | request.KeepAlive = true;
200 | request.AllowAutoRedirect = true;
201 | request.UnsafeAuthenticatedConnectionSharing = true;
202 | request.CookieContainer = new CookieContainer();
203 | //据说能提高性能
204 | request.Proxy = null;
205 | if(_cookiecollection != null)
206 | {
207 | foreach(Cookie c in _cookiecollection)
208 | {
209 | c.Domain = request.Host;
210 | }
211 |
212 | request.CookieContainer.Add(_cookiecollection);
213 | }
214 |
215 | foreach(KeyValuePair hd in _headers)
216 | {
217 | request.Headers[hd.Key] = hd.Value;
218 | }
219 |
220 | HttpWebResponse response = (HttpWebResponse)request.GetResponse();
221 | html = GetStringFromResponse(response);
222 | if(request.CookieContainer != null)
223 | {
224 | response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
225 | }
226 |
227 | if(response.Cookies != null)
228 | {
229 | _cookiecollection = response.Cookies;
230 | }
231 | if(response.Headers["Set-Cookie"] != null)
232 | {
233 | string tmpcookie = response.Headers["Set-Cookie"];
234 | _cookiecollection.Add(ConvertCookieString(tmpcookie));
235 | }
236 |
237 | response.Close();
238 | return html;
239 | }
240 | catch(Exception e)
241 | {
242 | Trace.WriteLine("HttpGet Error: " + e.Message);
243 | return String.Empty;
244 | }
245 | }
246 |
247 | ///
248 | /// 获取MINE文件
249 | ///
250 | ///
251 | ///
252 | public Byte[] HttpGetMine(String url)
253 | {
254 | Byte[] mine = null;
255 | try
256 | {
257 | ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckCertificate);
258 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
259 | request.UserAgent = _useragent;
260 | request.Timeout = _timeout;
261 | request.ContentType = _contenttype;
262 | request.Accept = _accept;
263 | request.Method = "GET";
264 | request.Referer = url;
265 | request.KeepAlive = true;
266 | request.AllowAutoRedirect = true;
267 | request.UnsafeAuthenticatedConnectionSharing = true;
268 | request.CookieContainer = new CookieContainer();
269 | //据说能提高性能
270 | request.Proxy = null;
271 | if(_cookiecollection != null)
272 | {
273 | foreach(Cookie c in _cookiecollection)
274 | c.Domain = request.Host;
275 | request.CookieContainer.Add(_cookiecollection);
276 | }
277 |
278 | foreach(KeyValuePair hd in _headers)
279 | {
280 | request.Headers[hd.Key] = hd.Value;
281 | }
282 |
283 | HttpWebResponse response = (HttpWebResponse)request.GetResponse();
284 | Stream stream = response.GetResponseStream();
285 | MemoryStream ms = new MemoryStream();
286 |
287 | byte[] b = new byte[1024];
288 | while(true)
289 | {
290 | int s = stream.Read(b,0,b.Length);
291 | ms.Write(b,0,s);
292 | if(s == 0 || s < b.Length)
293 | {
294 | break;
295 | }
296 | }
297 | mine = ms.ToArray();
298 | ms.Close();
299 |
300 | if(request.CookieContainer != null)
301 | {
302 | response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
303 | }
304 |
305 | if(response.Cookies != null)
306 | {
307 | _cookiecollection = response.Cookies;
308 | }
309 | if(response.Headers["Set-Cookie"] != null)
310 | {
311 | _cookies = response.Headers["Set-Cookie"];
312 | }
313 |
314 | stream.Close();
315 | stream.Dispose();
316 | response.Close();
317 | return mine;
318 | }
319 | catch(Exception e)
320 | {
321 | Trace.WriteLine("HttpGetMine Error: " + e.Message);
322 | return null;
323 | }
324 | }
325 |
326 | ///
327 | /// 发送POST请求
328 | ///
329 | ///
330 | ///
331 | ///
332 | public String HttpPost(String url,String data)
333 | {
334 | return HttpPost(url,data,url);
335 | }
336 |
337 | ///
338 | /// 发送POST请求
339 | ///
340 | ///
341 | ///
342 | ///
343 | ///
344 | public String HttpPost(String url,String data,String refer)
345 | {
346 | String html;
347 | try
348 | {
349 | ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckCertificate);
350 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
351 | request.UserAgent = _useragent;
352 | request.Timeout = _timeout;
353 | request.Referer = refer;
354 | request.ContentType = _contenttype;
355 | request.Accept = _accept;
356 | request.Method = "POST";
357 | request.KeepAlive = true;
358 | request.AllowAutoRedirect = true;
359 | request.CookieContainer = new CookieContainer();
360 | //据说能提高性能
361 | request.Proxy = null;
362 |
363 | if(_cookiecollection != null)
364 | {
365 | foreach(Cookie c in _cookiecollection)
366 | {
367 | c.Domain = request.Host;
368 | if(c.Domain.IndexOf(':') > 0)
369 | c.Domain = c.Domain.Remove(c.Domain.IndexOf(':'));
370 | }
371 | request.CookieContainer.Add(_cookiecollection);
372 | }
373 |
374 | foreach(KeyValuePair hd in _headers)
375 | {
376 | request.Headers[hd.Key] = hd.Value;
377 | }
378 | byte[] buffer = _encoding.GetBytes(data.Trim());
379 | request.ContentLength = buffer.Length;
380 | request.GetRequestStream().Write(buffer,0,buffer.Length);
381 | request.GetRequestStream().Close();
382 |
383 | HttpWebResponse response = (HttpWebResponse)request.GetResponse();
384 | html = GetStringFromResponse(response);
385 | if(request.CookieContainer != null)
386 | {
387 | response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
388 | }
389 | if(response.Cookies != null)
390 | {
391 | _cookiecollection = response.Cookies;
392 | }
393 | if(response.Headers["Set-Cookie"] != null)
394 | {
395 | string tmpcookie = response.Headers["Set-Cookie"];
396 | _cookiecollection.Add(ConvertCookieString(tmpcookie));
397 | }
398 |
399 | response.Close();
400 | return html;
401 | }
402 | catch(Exception e)
403 | {
404 | Trace.WriteLine("HttpPost Error: " + e.Message);
405 | return String.Empty;
406 | }
407 | }
408 |
409 |
410 | public string UrlEncode(string str)
411 | {
412 | StringBuilder sb = new StringBuilder();
413 | byte[] byStr = _encoding.GetBytes(str);
414 | for (int i = 0; i < byStr.Length; i++)
415 | {
416 | sb.Append(@"%" + Convert.ToString(byStr[i], 16));
417 | }
418 |
419 | return (sb.ToString());
420 | }
421 |
422 | ///
423 | /// 转换cookie字符串到CookieCollection
424 | ///
425 | ///
426 | ///
427 | private CookieCollection ConvertCookieString(string ck)
428 | {
429 | CookieCollection cc = new CookieCollection();
430 | string[] cookiesarray = ck.Split(";".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
431 | for(int i = 0;i < cookiesarray.Length;i++)
432 | {
433 | string[] cookiesarray_2 = cookiesarray[i].Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
434 | for(int j = 0;j < cookiesarray_2.Length;j++)
435 | {
436 | string[] cookiesarray_3 = cookiesarray_2[j].Trim().Split("=".ToCharArray());
437 | if(cookiesarray_3.Length == 2)
438 | {
439 | string cname = cookiesarray_3[0].Trim();
440 | string cvalue = cookiesarray_3[1].Trim();
441 | if(cname.ToLower() != "domain" && cname.ToLower() != "path" && cname.ToLower() != "expires")
442 | {
443 | Cookie c = new Cookie(cname,cvalue);
444 | cc.Add(c);
445 | }
446 | }
447 | }
448 | }
449 |
450 | return cc;
451 | }
452 |
453 |
454 | public void DebugCookies()
455 | {
456 | Trace.WriteLine("**********************BEGIN COOKIES*************************");
457 | foreach(Cookie c in _cookiecollection)
458 | {
459 | Trace.WriteLine(c.Name + "=" + c.Value);
460 | Trace.WriteLine("Path=" + c.Path);
461 | Trace.WriteLine("Domain=" + c.Domain);
462 | }
463 | Trace.WriteLine("**********************END COOKIES*************************");
464 | }
465 |
466 | }
467 | }
468 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HttpHelper
2 | 基于HttpWebRequest的Http客户端
3 |
4 | 特性:
5 | *支持http/https
6 | *支持跨域Cookies
7 |
--------------------------------------------------------------------------------