-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtility.cs
259 lines (237 loc) · 6.65 KB
/
HttpUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using HttpHelper = System.Web.HttpUtility;
using RequestParams = System.Collections.Generic.Dictionary<string, object>;
public class HttpUtility
{
public static Encoding defaultEncoding;
/// <summary>
/// 设置连接数限制,代理
/// </summary>
public static void Setup(int limit, IWebProxy proxy = null)
{
ServicePointManager.DefaultConnectionLimit = limit;
WebRequest.DefaultWebProxy = proxy;
}
/// <summary>
/// HTTP GET
/// </summary>
public static string GetText(string url, RequestParams data = null,
Action<HttpWebRequest> action = null)
{
var req = _BuildGetRequest(url, data, action);
return _GetResponseText(req);
}
/// <summary>
/// HTTP GET, copy to output
/// </summary>
public static void GetStream(string url, Stream output, RequestParams data = null,
Action<HttpWebRequest> action = null)
{
var req = _BuildGetRequest(url, data, action);
_WriteResponseToStream(req, output);
}
/// <summary>
/// HTTP GET, copy to output
/// </summary>
public static void GetStream(string url, Stream output, Action<int, int> progReporter,
RequestParams data = null, Action<HttpWebRequest> action = null)
{
var req = _BuildGetRequest(url, data, action);
_WriteResponseToStream(req, output, progReporter);
}
/// <summary>
/// HTTP POST (x-www-form-urlencoded)
/// </summary>
public static string PostText(string url, RequestParams data = null,
Action<HttpWebRequest> action = null)
{
var req = _BuildPostRequest(url, data, action);
return _GetResponseText(req);
}
/// <summary>
/// HTTP POST (x-www-form-urlencoded), copy to output
/// </summary>
public static void PostStream(string url, Stream output, RequestParams data = null,
Action<HttpWebRequest> action = null)
{
var req = _BuildPostRequest(url, data, action);
_WriteResponseToStream(req, output);
}
/// <summary>
/// 从text的offset处开始,寻找介于start和end之间的字符串
/// </summary>
public static string FindBetween(string text, string start, string end, ref int offset)
{
if (offset >= text.Length)
return null;
int p_start = text.IndexOf(start, offset);
if (p_start < 0)
return null;
p_start += start.Length;
if (p_start >= text.Length)
return null;
int p_end = text.IndexOf(end, p_start);
if (p_end < 0)
return null;
offset = p_end + end.Length;
return text.Substring(p_start, p_end - p_start);
}
/// <summary>
/// 从text中寻找介于start和end之间的字符串
/// </summary>
public static string FindBetween(string text, string start, string end)
{
int offset = 0;
return FindBetween(text, start, end, ref offset);
}
/// <summary>
/// 从text中寻找介于start和end之间的字符串
/// </summary>
public static IEnumerable<string> FindAllBetween(string text, string start, string end)
{
int offset = 0;
string found = null;
do
{
found = FindBetween(text, start, end, ref offset);
if (found != null)
yield return found;
}
while (found != null);
}
/// <summary>
/// 构造Get请求
/// </summary>
static HttpWebRequest _BuildGetRequest(string url, RequestParams data,
Action<HttpWebRequest> action)
{
//如果传入了data,则先构造请求url
if (data != null && data.Count > 0)
{
StringBuilder sb = new StringBuilder(url);
if (url.IndexOf('?') < 0)
sb.Append('?');
//build request
foreach (var entry in data)
{
object v = entry.Value;
sb.Append(HttpHelper.UrlEncode(entry.Key))
.Append('=')
.Append(v == null ? "" : HttpHelper.UrlEncode(v.ToString()))
.Append('&');
}
//change url
url = sb.ToString(0, sb.Length - 1); //remove last '&'
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
action?.Invoke(req);
return req;
}
/// <summary>
/// 构造Post请求 (x-www-form-urlencoded)
/// </summary>
static HttpWebRequest _BuildPostRequest(string url, RequestParams data,
Action<HttpWebRequest> action)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
action?.Invoke(req);
//写入数据
if (data != null && data.Count > 0)
{
//Build request
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
{
bool first = true;
foreach (var entry in data)
{
if (!first)
writer.Write('&');
else
first = false;
object v = entry.Value;
writer.Write(HttpHelper.UrlEncode(entry.Key));
writer.Write('=');
if (v != null)
writer.Write(HttpHelper.UrlEncode(v.ToString()));
}
}
}
return req;
}
/// <summary>
/// 返回请求结果
/// </summary>
static string _GetResponseText(HttpWebRequest req)
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
if (!_PreCheckResponse(resp))
return "";
Encoding enc;
if (defaultEncoding != null)
enc = defaultEncoding;
else
{
string charSet = resp.CharacterSet;
enc = string.IsNullOrEmpty(charSet) ? Encoding.UTF8
: Encoding.GetEncoding(charSet);
}
using (StreamReader reader = new StreamReader(resp.GetResponseStream(), enc))
return reader.ReadToEnd();
}
}
/// <summary>
/// 将请求结果写入stream
/// </summary>
static void _WriteResponseToStream(HttpWebRequest req, Stream stream)
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
if (!_PreCheckResponse(resp))
return;
using (Stream respStream = resp.GetResponseStream())
respStream.CopyTo(stream);
}
}
/// <summary>
/// 将请求结果写入stream
/// </summary>
static void _WriteResponseToStream(HttpWebRequest req, Stream stream, Action<int, int> progReporter)
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
if (!_PreCheckResponse(resp))
return;
var length = (int)resp.ContentLength;
using (Stream respStream = resp.GetResponseStream())
{
var buffer = new byte[4096];
var totalRead = 0;
int read;
do
{
read = respStream.Read(buffer, 0, 4096);
totalRead += read;
stream.Write(buffer, 0, read);
progReporter(totalRead, length);
}
while (read > 0);
}
}
}
/// <summary>
/// 检查返回的状态是否为2xx
/// </summary>
static bool _PreCheckResponse(HttpWebResponse resp)
{
int status = (int)resp.StatusCode;
return status >= 200 && status <= 299;
}
}