/
RequestUtility.Post.cs
571 lines (467 loc) · 22.8 KB
/
RequestUtility.Post.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2018 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0
/*----------------------------------------------------------------
Copyright (C) 2018 Senparc
文件名:RequestUtility.Post.cs
文件功能描述:获取请求结果(Post)
创建标识:Senparc - 20171006
修改描述:移植Post方法过来
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Senparc.Weixin.Helpers;
#if NET35 || NET40 || NET45
using System.Web;
#else
using System.Net.Http;
using System.Net.Http.Headers;
#endif
#if NETSTANDARD1_6 || NETSTANDARD2_0 || NETCOREAPP2_0
using Microsoft.AspNetCore.Http;
using Senparc.Weixin.WebProxy;
#endif
namespace Senparc.Weixin.HttpUtility
{
/// <summary>
/// HTTP 请求工具类
/// </summary>
public static partial class RequestUtility
{
#region 静态公共方法
#if NET35 || NET40 || NET45
/// <summary>
/// 给.NET Framework使用的HttpPost请求公共设置方法
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="postStream"></param>
/// <param name="fileDictionary"></param>
/// <param name="refererUrl"></param>
/// <param name="encoding"></param>
/// <param name="cer"></param>
/// <param name="useAjax"></param>
/// <param name="timeOut"></param>
/// <param name="checkValidationResult"></param>
/// <returns></returns>
public static HttpWebRequest HttpPost_Common_Net45(string url, CookieContainer cookieContainer = null,
Stream postStream = null, Dictionary<string, string> fileDictionary = null, string refererUrl = null,
Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT,
bool checkValidationResult = false)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Timeout = timeOut;
request.Proxy = _webproxy;
if (cer != null)
{
request.ClientCertificates.Add(cer);
}
if (checkValidationResult)
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CheckValidationResult);
}
#region 处理Form表单文件上传
var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件
if (formUploadFile)
{
//通过表单上传文件
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
postStream = postStream ?? new MemoryStream();
//byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
string dataFormdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (var file in fileDictionary)
{
try
{
var fileName = file.Value;
//准备文件流
using (var fileStream = FileHelper.GetFileStream(fileName))
{
string formdata = null;
if (fileStream != null)
{
//存在文件
formdata = string.Format(fileFormdataTemplate, file.Key, /*fileName*/ Path.GetFileName(fileName));
}
else
{
//不存在文件或只是注释
formdata = string.Format(dataFormdataTemplate, file.Key, file.Value);
}
//统一处理
var formdataBytes = Encoding.UTF8.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行
postStream.Write(formdataBytes, 0, formdataBytes.Length);
//写入文件
if (fileStream != null)
{
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
postStream.Write(buffer, 0, bytesRead);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
//结尾
var footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
postStream.Write(footer, 0, footer.Length);
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
}
else
{
request.ContentType = "application/x-www-form-urlencoded";
}
#endregion
request.ContentLength = postStream != null ? postStream.Length : 0;
HttpClientHeader(request, refererUrl, useAjax, timeOut);
if (cookieContainer != null)
{
request.CookieContainer = cookieContainer;
}
return request;
}
#endif
#if NETSTANDARD1_6 || NETSTANDARD2_0 || NETCOREAPP2_0
/// <summary>
/// 给.NET Core使用的HttpPost请求公共设置方法
/// </summary>
/// <param name="url"></param>
/// <param name="hc"></param>
/// <param name="cookieContainer"></param>
/// <param name="postStream"></param>
/// <param name="fileDictionary"></param>
/// <param name="refererUrl"></param>
/// <param name="encoding"></param>
/// <param name="cer"></param>
/// <param name="useAjax"></param>
/// <param name="timeOut"></param>
/// <param name="checkValidationResult"></param>
/// <returns></returns>
public static HttpClient HttpPost_Common_NetCore(string url, out HttpContent hc, CookieContainer cookieContainer = null,
Stream postStream = null, Dictionary<string, string> fileDictionary = null, string refererUrl = null,
Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT,
bool checkValidationResult = false)
{
var handler = new HttpClientHandler()
{
UseProxy = _webproxy != null,
Proxy = _webproxy,
UseCookies = true,
CookieContainer = cookieContainer,
};
if (checkValidationResult)
{
handler.ServerCertificateCustomValidationCallback = new Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool>(CheckValidationResult);
}
if (cer != null)
{
handler.ClientCertificates.Add(cer);
}
HttpClient client = new HttpClient(handler);
HttpClientHeader(client, refererUrl, useAjax, timeOut);
#region 处理Form表单文件上传
var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件
if (formUploadFile)
{
//通过表单上传文件
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
hc = new MultipartFormDataContent(boundary);
foreach (var file in fileDictionary)
{
try
{
var fileName = file.Value;
//准备文件流
using (var fileStream = FileHelper.GetFileStream(fileName))
{
if (fileStream != null)
{
//存在文件
//hc.Add(new StreamContent(fileStream), file.Key, Path.GetFileName(fileName)); //报流已关闭的异常
fileStream.Dispose();
(hc as MultipartFormDataContent).Add(CreateFileContent(File.Open(fileName, FileMode.Open), Path.GetFileName(fileName)), file.Key, Path.GetFileName(fileName));
}
else
{
//不存在文件或只是注释
(hc as MultipartFormDataContent).Add(new StringContent(string.Empty), file.Key, file.Value);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
hc.Headers.ContentType = MediaTypeHeaderValue.Parse(string.Format("multipart/form-data; boundary={0}", boundary));
}
else
{
hc = new StreamContent(postStream);
//使用Url格式Form表单Post提交的时候才使用application/x-www-form-urlencoded
//hc.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
hc.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
}
//HttpContentHeader(hc, timeOut);
#endregion
if (!string.IsNullOrEmpty(refererUrl))
{
client.DefaultRequestHeaders.Referrer = new Uri(refererUrl);
}
return client;
}
#endif
#endregion
#region 同步方法
/// <summary>
/// 使用Post方法获取字符串结果,常规提交
/// </summary>
/// <returns></returns>
public static string HttpPost(string url, CookieContainer cookieContainer = null, Dictionary<string, string> formData = null, Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT)
{
MemoryStream ms = new MemoryStream();
formData.FillFormDataStream(ms);//填充formData
return HttpPost(url, cookieContainer, ms, null, null, encoding, cer, useAjax, timeOut);
}
/// <summary>
/// 使用Post方法获取字符串结果
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="postStream"></param>
/// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param>
/// <param name="encoding"></param>
/// <param name="cer">证书,如果不需要则保留null</param>
/// <param name="useAjax"></param>
/// <param name="timeOut"></param>
/// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
/// <param name="refererUrl"></param>
/// <returns></returns>
public static string HttpPost(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary<string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT, bool checkValidationResult = false)
{
if (cookieContainer == null)
{
cookieContainer = new CookieContainer();
}
#if NET35 || NET40 || NET45
#region 已经使用方法重用
/*
var request = HttpPost_Common_Net45(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
#region 输入二进制流
if (postStream != null)
{
postStream.Position = 0;
//直接写入流
Stream requestStream = request.GetRequestStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
//debug
//postStream.Seek(0, SeekOrigin.Begin);
//StreamReader sr = new StreamReader(postStream);
//var postStr = sr.ReadToEnd();
postStream.Close();//关闭文件访问
}
#endregion
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
*/
#endregion
var senparcResponse = HttpResponsePost(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
HttpWebResponse response = senparcResponse.Result;
response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
using (Stream responseStream = response.GetResponseStream() ?? new MemoryStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8")))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
#else
var senparcResponse = HttpResponsePost(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
var response = senparcResponse.Result;
if (response.Content.Headers.ContentType.CharSet != null &&
response.Content.Headers.ContentType.CharSet.ToLower().Contains("utf8"))
{
response.Content.Headers.ContentType.CharSet = "utf-8";
}
var retString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return retString;
//t1.Wait();
//return t1.Result;
#endif
}
/// <summary>
/// 使用Post方法获取HttpWebResponse或HttpResponseMessage对象,本方法独立使用时通常用于测试)
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="postStream"></param>
/// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param>
/// <param name="encoding"></param>
/// <param name="cer">证书,如果不需要则保留null</param>
/// <param name="useAjax"></param>
/// <param name="timeOut"></param>
/// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
/// <param name="refererUrl"></param>
/// <returns></returns>
public static SenparcHttpResponse HttpResponsePost(string url, CookieContainer cookieContainer = null, Stream postStream = null,
Dictionary<string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null,
X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT,
bool checkValidationResult = false)
{
if (cookieContainer == null)
{
cookieContainer = new CookieContainer();
}
postStream = postStream ?? new MemoryStream();
#if NET35 || NET40 || NET45
var request = HttpPost_Common_Net45(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
#region 输入二进制流
if (postStream != null && postStream.Length > 0)
{
postStream.Position = 0;
//直接写入流
Stream requestStream = request.GetRequestStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
//debug
//postStream.Seek(0, SeekOrigin.Begin);
//StreamReader sr = new StreamReader(postStream);
//var postStr = sr.ReadToEnd();
postStream.Close();//关闭文件访问
}
#endregion
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return new SenparcHttpResponse(response);
#else
HttpContent hc;
var client = HttpPost_Common_NetCore(url, out hc, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
var response = client.PostAsync(url, hc).GetAwaiter().GetResult();
return new SenparcHttpResponse(response);
#endif
}
#endregion
#if !NET35 && !NET40
#region 异步方法
/// <summary>
/// 使用Post方法获取字符串结果,常规提交
/// </summary>
/// <returns></returns>
public static async Task<string> HttpPostAsync(string url, CookieContainer cookieContainer = null, Dictionary<string, string> formData = null, Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, int timeOut = Config.TIME_OUT)
{
#if NET35 || NET40 || NET45
MemoryStream ms = new MemoryStream();
await formData.FillFormDataStreamAsync(ms);//填充formData
return await HttpPostAsync(url, cookieContainer, ms, null, null, encoding, cer, useAjax, timeOut);
#else
MemoryStream ms = new MemoryStream();
await formData.FillFormDataStreamAsync(ms);//填充formData
return await HttpPostAsync(url, cookieContainer, ms, null, null, encoding, cer, useAjax, timeOut);
#endif
}
/// <summary>
/// 使用Post方法获取字符串结果
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="postStream"></param>
/// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param>
/// <param name="cer"></param>
/// <param name="useAjax"></param>
/// <param name="timeOut"></param>
/// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
/// <param name="refererUrl"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static async Task<string> HttpPostAsync(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary<string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, X509Certificate2 cer = null,
bool useAjax = false, int timeOut = Config.TIME_OUT, bool checkValidationResult = false)
{
if (cookieContainer == null)
{
cookieContainer = new CookieContainer();
}
postStream = postStream ?? new MemoryStream();
#if NET35 || NET40 || NET45
var request = HttpPost_Common_Net45(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
#region 输入二进制流
if (postStream != null && postStream.Length > 0)
{
postStream.Position = 0;
//直接写入流
Stream requestStream = await request.GetRequestStreamAsync();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = await postStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await requestStream.WriteAsync(buffer, 0, bytesRead);
}
//debug
//postStream.Seek(0, SeekOrigin.Begin);
//StreamReader sr = new StreamReader(postStream);
//var postStr = await sr.ReadToEndAsync();
postStream.Close();//关闭文件访问
}
#endregion
HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync());
if (cookieContainer != null)
{
response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8")))
{
string retString = await myStreamReader.ReadToEndAsync();
return retString;
}
}
#else
HttpContent hc;
var client = HttpPost_Common_NetCore(url, out hc, cookieContainer, postStream, fileDictionary, refererUrl, encoding, cer, useAjax, timeOut, checkValidationResult);
var r = await client.PostAsync(url, hc);
if (r.Content.Headers.ContentType.CharSet != null &&
r.Content.Headers.ContentType.CharSet.ToLower().Contains("utf8"))
{
r.Content.Headers.ContentType.CharSet = "utf-8";
}
return await r.Content.ReadAsStringAsync();
#endif
}
#endregion
#endif
}
}