1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Net;
6 using System.Text;
7
8 namespace HelperLib
9 {
10 public enum ResponeType
11 {
12 String,
13 File
14 }
15 /// <summary>
16 /// HttpRequest Help
17 /// Code By:lvxiaojia
18 /// blog:http://www.cnblogs.com/lvxiaojia/
19 /// </summary>
20 public class RequestHelp
21 {
22 static CookieContainer cookie = new CookieContainer();
23 public static string Post(string url, Dictionary<string, string> postData, string referer = "", string accept = "", string contentType = "", ResponeType type = ResponeType.String, string fileSavePath = "", Action<string> action = null, Func<Dictionary<string, string>> fun = null)
24 {
25 var result = "";
26 //var cookie = new CookieContainer();
27 StringBuilder strPostData = new StringBuilder();
28 if (postData != null)
29 {
30 postData.AsQueryable().ToList().ForEach(a =>
31 {
32 strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
33 });
34 }
35 byte[] byteArray = Encoding.UTF8.GetBytes(strPostData.ToString().TrimEnd('&'));
36
37 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
38
39 webRequest.CookieContainer = cookie;
40
41 webRequest.Method = "POST";
42 if (string.IsNullOrEmpty(accept))
43 webRequest.Accept = "application/json, text/javascript, */*;";
44 else
45 webRequest.Accept = accept;
46
47 if (!string.IsNullOrEmpty(referer))
48 webRequest.Referer = referer;
49 if (string.IsNullOrEmpty(contentType))
50 webRequest.ContentType = "application/x-www-form-urlencoded";
51 else
52 webRequest.ContentType = contentType;
53
54 if (strPostData.Length > 0)
55 webRequest.ContentLength = byteArray.Length;
56
57 //请求
58 Stream newStream = webRequest.GetRequestStream();
59 newStream.Write(byteArray, 0, byteArray.Length);
60 newStream.Close();
61
62 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
63 var responSteam = response.GetResponseStream();
64
65 if (type == ResponeType.String)
66 {
67 StreamReader strRespon = new StreamReader(responSteam, Encoding.UTF8);
68 result = strRespon.ReadToEnd();
69 }
70 else
71 {
72 BinaryReader br = new BinaryReader(responSteam);
73 byte[] byteArr = br.ReadBytes(200000);
74 FileStream fs = new FileStream(fileSavePath, FileMode.OpenOrCreate);
75 fs.Write(byteArr, 0, byteArr.Length);
76 fs.Dispose();
77 fs.Close();
78 result = "OK";
79 }
80 if (action != null)
81 {
82 action.Invoke(result);
83 }
84 if (fun != null)
85 {
86 Dictionary<string, string> dic = new Dictionary<string, string>();
87 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
88 {
89 var c = item as Cookie;
90 dic.Add(c.Name, c.Value);
91 }
92 fun = () => { return dic; };
93 }
94 return result;
95
96 }
97
98
99 public static string Get(string url, Dictionary<string, string> postData=null, string referer = "", Action<string> action = null, Action<Dictionary<string, string>> fun = null)
100 {
101 var result = "";
102
103 StringBuilder strPostData = new StringBuilder("?");
104 if (postData != null)
105 {
106 postData.AsQueryable().ToList().ForEach(a =>
107 {
108 strPostData.AppendFormat("{0}={1}&", a.Key, a.Value);
109 });
110 }
111 if (strPostData.Length == 1)
112 strPostData = strPostData.Clear();
113 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + strPostData.ToString().TrimEnd('&'));
114 webRequest.CookieContainer = cookie;
115 webRequest.Method = "GET";
116 webRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*;";
117 if (!string.IsNullOrEmpty(referer))
118 webRequest.Referer = referer;
119 //请求
120 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
121 var responSteam = response.GetResponseStream();
122
123 StreamReader strRespon = new StreamReader(responSteam, Encoding.Default);
124 result = strRespon.ReadToEnd();
125
126 if (action != null)
127 {
128 action.Invoke(result);
129 }
130 if (fun != null)
131 {
132 Dictionary<string, string> dic = new Dictionary<string, string>();
133 foreach (var item in cookie.GetCookies(webRequest.RequestUri))
134 {
135 var c = item as Cookie;
136 dic.Add(c.Name, c.Value);
137 }
138 fun.Invoke(dic);
139 }
140 return result;
141
142 }
143
144 }
145 }