|
@@ -1,5 +1,8 @@
|
1
|
|
-using System;
|
|
1
|
+using Newtonsoft.Json.Linq;
|
|
2
|
+
|
|
3
|
+using System;
|
2
|
4
|
using System.Collections.Generic;
|
|
5
|
+using System.Collections.Specialized;
|
3
|
6
|
using System.IO;
|
4
|
7
|
using System.Net;
|
5
|
8
|
using System.Net.Security;
|
|
@@ -109,7 +112,7 @@ namespace Common.system
|
109
|
112
|
{
|
110
|
113
|
try
|
111
|
114
|
{
|
112
|
|
- var request = GetHttpWebRequest(url);
|
|
115
|
+ HttpWebRequest request = GetHttpWebRequest(url);
|
113
|
116
|
if (request != null)
|
114
|
117
|
{
|
115
|
118
|
string retval = null;
|
|
@@ -117,7 +120,7 @@ namespace Common.system
|
117
|
120
|
request.Method = "POST";
|
118
|
121
|
request.ServicePoint.Expect100Continue = false;
|
119
|
122
|
request.ContentType = "application/json; charset=utf-8";
|
120
|
|
- request.Timeout = 800;
|
|
123
|
+ request.Timeout = 5000;
|
121
|
124
|
var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(data);
|
122
|
125
|
request.ContentLength = bytes.Length;
|
123
|
126
|
using (var stream = request.GetRequestStream())
|
|
@@ -163,7 +166,7 @@ namespace Common.system
|
163
|
166
|
string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
|
164
|
167
|
return JsonHelper.JsonToObj<T>(respstr);
|
165
|
168
|
}
|
166
|
|
- catch (Exception)
|
|
169
|
+ catch (Exception ex)
|
167
|
170
|
{
|
168
|
171
|
}
|
169
|
172
|
finally
|
|
@@ -193,20 +196,354 @@ namespace Common.system
|
193
|
196
|
}
|
194
|
197
|
private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType)
|
195
|
198
|
{
|
196
|
|
- byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
|
197
|
|
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
|
198
|
|
- webRequest.Method = "POST";
|
199
|
|
- webRequest.ContentType = contentType + ";" + encode;
|
200
|
|
- webRequest.ContentLength = data.Length;
|
201
|
|
- webRequest.Timeout = timeout;
|
202
|
|
- requestStream = webRequest.GetRequestStream();
|
203
|
|
- requestStream.Write(data, 0, data.Length);
|
204
|
|
- webResponse = (HttpWebResponse)webRequest.GetResponse();
|
205
|
|
- responseStream = webResponse.GetResponseStream();
|
206
|
|
- if (responseStream == null) { return ""; }
|
207
|
|
- streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
|
208
|
|
- return streamReader.ReadToEnd();
|
|
199
|
+ try
|
|
200
|
+ {
|
|
201
|
+ byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
|
|
202
|
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
203
|
+ webRequest.Method = "POST";
|
|
204
|
+ webRequest.ContentType = contentType + ";" + encode;
|
|
205
|
+ webRequest.ContentLength = data.Length;
|
|
206
|
+ webRequest.Timeout = timeout;
|
|
207
|
+ requestStream = webRequest.GetRequestStream();
|
|
208
|
+ requestStream.Write(data, 0, data.Length);
|
|
209
|
+ webResponse = (HttpWebResponse)webRequest.GetResponse();
|
|
210
|
+ responseStream = webResponse.GetResponseStream();
|
|
211
|
+ if (responseStream == null) { return ""; }
|
|
212
|
+ streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
|
|
213
|
+ return streamReader.ReadToEnd();
|
|
214
|
+ }
|
|
215
|
+ catch (Exception ex)
|
|
216
|
+ {
|
|
217
|
+ return null;
|
|
218
|
+ }
|
209
|
219
|
}
|
|
220
|
+ /// <summary>
|
|
221
|
+ /// Cookie
|
|
222
|
+ /// </summary>
|
|
223
|
+ public static CookieContainer cc = new CookieContainer();
|
|
224
|
+ /// <summary>
|
|
225
|
+ /// Get请求
|
|
226
|
+ /// </summary>
|
|
227
|
+ /// <param name="serviceaddress">请求地址</param>
|
|
228
|
+ /// <param name="strcontent">头</param>
|
|
229
|
+ /// <param name="contenttype">参数</param>
|
|
230
|
+ /// <param name="header">token</param>
|
|
231
|
+ /// <returns></returns>
|
|
232
|
+ public static JObject GetFunction(string serviceaddress, string strcontent, string contenttype, string header, int Timeout = 5000)
|
|
233
|
+ {
|
|
234
|
+ try
|
|
235
|
+ {
|
|
236
|
+ string serviceAddress = serviceaddress + strcontent;
|
|
237
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
238
|
+ WebHeaderCollection myWebHeaderCollection = request.Headers;
|
|
239
|
+ request.CookieContainer = cc;
|
|
240
|
+ if (header != "")
|
|
241
|
+ {
|
|
242
|
+ myWebHeaderCollection.Add("access_token:" + header);
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ request.Method = "GET";
|
|
246
|
+ request.ContentType = contenttype;
|
|
247
|
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
248
|
+
|
|
249
|
+ string encoding = response.ContentEncoding;
|
|
250
|
+ if (encoding == null || encoding.Length < 1)
|
|
251
|
+ {
|
|
252
|
+ encoding = "UTF-8"; //默认编码
|
|
253
|
+ }
|
|
254
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
|
255
|
+ string retString = reader.ReadToEnd();
|
210
|
256
|
|
|
257
|
+ //解析josn
|
|
258
|
+ JObject jo = JObject.Parse(retString);
|
|
259
|
+ return jo;
|
|
260
|
+ }
|
|
261
|
+ catch (Exception)
|
|
262
|
+ {
|
|
263
|
+ return null;
|
|
264
|
+ }
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+ /// <summary>
|
|
268
|
+ /// Post请求
|
|
269
|
+ /// </summary>
|
|
270
|
+ /// <param name="serviceaddress">请求地址</param>
|
|
271
|
+ /// <param name="contenttype">头 application/x-www-form-urlencoded</param>
|
|
272
|
+ /// <param name="strcontent">参数</param>
|
|
273
|
+ /// <param name="header">token</param>
|
|
274
|
+ /// <returns></returns>
|
|
275
|
+ public static JObject PostFunction(string serviceaddress, string contenttype, string strcontent, string header, int Timeout = 5000)
|
|
276
|
+ {
|
|
277
|
+ try
|
|
278
|
+ {
|
|
279
|
+ string serviceAddress = serviceaddress;
|
|
280
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
281
|
+ WebHeaderCollection myWebHeaderCollection = request.Headers;
|
|
282
|
+
|
|
283
|
+ request.CookieContainer = cc;
|
|
284
|
+ //myWebHeaderCollection.Add("Accept", "*/*");
|
|
285
|
+ if (header != "")
|
|
286
|
+ {
|
|
287
|
+ myWebHeaderCollection.Add("access_token:" + header);
|
|
288
|
+ }
|
|
289
|
+ request.Timeout = Timeout;
|
|
290
|
+ request.Method = "POST";
|
|
291
|
+ request.ContentType = contenttype;
|
|
292
|
+
|
|
293
|
+ string strContent = strcontent;
|
|
294
|
+ using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
|
|
295
|
+ {
|
|
296
|
+ dataStream.Write(strContent);
|
|
297
|
+ dataStream.Close();
|
|
298
|
+ }
|
|
299
|
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
300
|
+ string encoding = response.ContentEncoding;
|
|
301
|
+ if (encoding == null || encoding.Length < 1)
|
|
302
|
+ {
|
|
303
|
+ encoding = "UTF-8"; //默认编码
|
|
304
|
+ }
|
|
305
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
|
306
|
+ string retString = reader.ReadToEnd();
|
|
307
|
+ retString = retString.Replace("\\r\\n", "");
|
|
308
|
+
|
|
309
|
+ //解析josn
|
|
310
|
+ JObject jo = JObject.Parse(retString);
|
|
311
|
+ return jo;
|
|
312
|
+ }
|
|
313
|
+ catch (Exception ex)
|
|
314
|
+ {
|
|
315
|
+ LogHelper.WriteErrLog("请求失败(若网络无问题,请重置winsock,解决方法:以管理员方式打开cmd执行 netsh winsock reset 后重启)", ex);
|
|
316
|
+ return null;
|
|
317
|
+ }
|
|
318
|
+
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ /// <summary>
|
|
322
|
+ /// 上传方法
|
|
323
|
+ /// </summary>
|
|
324
|
+ /// <param name="url">webapi地址</param>
|
|
325
|
+ /// <param name="files">本地文件路径,单文件默认为string[0]</param>
|
|
326
|
+ /// <param name="token"></param>
|
|
327
|
+ /// <param name="formFields">参数可不传</param>
|
|
328
|
+ /// <returns></returns>
|
|
329
|
+ public static JObject UploadFilesToRemoteUrl(string url, string[] files, string token, NameValueCollection formFields = null)
|
|
330
|
+ {
|
|
331
|
+ string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
|
|
332
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
333
|
+ request.ContentType = "multipart/form-data; boundary=" +
|
|
334
|
+ boundary;
|
|
335
|
+ request.Method = "POST";
|
|
336
|
+ request.KeepAlive = true;
|
|
337
|
+
|
|
338
|
+ Stream memStream = new System.IO.MemoryStream();
|
|
339
|
+
|
|
340
|
+ var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
|
|
341
|
+ boundary + "\r\n");
|
|
342
|
+ var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
|
|
343
|
+ boundary + "--");
|
|
344
|
+ WebHeaderCollection myWebHeaderCollection = request.Headers;
|
|
345
|
+ request.CookieContainer = cc;
|
|
346
|
+ if (token != "")
|
|
347
|
+ {
|
|
348
|
+ myWebHeaderCollection.Add("access_token:" + token);
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ string formdataTemplate = "\r\n--" + boundary +
|
|
352
|
+ "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
|
|
353
|
+ if (formFields != null)
|
|
354
|
+ {
|
|
355
|
+ foreach (string key in formFields.Keys)
|
|
356
|
+ {
|
|
357
|
+ string formitem = string.Format(formdataTemplate, key, formFields[key]);
|
|
358
|
+ byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
|
|
359
|
+ memStream.Write(formitembytes, 0, formitembytes.Length);
|
|
360
|
+ }
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ string headerTemplate =
|
|
364
|
+ "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
|
|
365
|
+ "Content-Type: application/octet-stream\r\n\r\n";
|
|
366
|
+
|
|
367
|
+ for (int i = 0; i < files.Length; i++)
|
|
368
|
+ {
|
|
369
|
+ memStream.Write(boundarybytes, 0, boundarybytes.Length);
|
|
370
|
+ var header = string.Format(headerTemplate, "file", files[i]);
|
|
371
|
+ var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
|
|
372
|
+
|
|
373
|
+ memStream.Write(headerbytes, 0, headerbytes.Length);
|
|
374
|
+
|
|
375
|
+ using (var fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
|
|
376
|
+ {
|
|
377
|
+ var buffer = new byte[1024];
|
|
378
|
+ var bytesRead = 0;
|
|
379
|
+ while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
|
380
|
+ {
|
|
381
|
+ memStream.Write(buffer, 0, bytesRead);
|
|
382
|
+ }
|
|
383
|
+ }
|
|
384
|
+ }
|
|
385
|
+
|
|
386
|
+ //memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
|
387
|
+ request.ContentLength = memStream.Length;
|
|
388
|
+
|
|
389
|
+ using (Stream requestStream = request.GetRequestStream())
|
|
390
|
+ {
|
|
391
|
+ memStream.Position = 0;
|
|
392
|
+ byte[] tempBuffer = new byte[memStream.Length];
|
|
393
|
+ memStream.Read(tempBuffer, 0, tempBuffer.Length);
|
|
394
|
+ memStream.Close();
|
|
395
|
+ requestStream.Write(tempBuffer, 0, tempBuffer.Length);
|
|
396
|
+ }
|
|
397
|
+
|
|
398
|
+ using (var response = request.GetResponse())
|
|
399
|
+ {
|
|
400
|
+ Stream stream2 = response.GetResponseStream();
|
|
401
|
+ StreamReader reader2 = new StreamReader(stream2);
|
|
402
|
+ JObject a = JObject.Parse(reader2.ReadToEnd());
|
|
403
|
+ return a;
|
|
404
|
+ }
|
|
405
|
+ }
|
|
406
|
+
|
|
407
|
+ /// <summary>
|
|
408
|
+ /// HttpWebRequest 下载
|
|
409
|
+ /// </summary>
|
|
410
|
+ /// <param name="url">URI</param>
|
|
411
|
+ /// <returns></returns>
|
|
412
|
+ public static bool GetDataGetHtml(string url, string filePath, string header)
|
|
413
|
+ {
|
|
414
|
+ try
|
|
415
|
+ {
|
|
416
|
+ HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
417
|
+
|
|
418
|
+ //httpWebRequest.ContentType = "application/x-www-form-urlencoded";
|
|
419
|
+ httpWebRequest.Method = "GET";
|
|
420
|
+ //httpWebRequest.ContentType = "image/jpeg";
|
|
421
|
+ //对发送的数据不使用缓存
|
|
422
|
+ httpWebRequest.AllowWriteStreamBuffering = false;
|
|
423
|
+ //httpWebRequest.Timeout = 300000;
|
|
424
|
+ httpWebRequest.ServicePoint.Expect100Continue = false;
|
|
425
|
+
|
|
426
|
+ WebHeaderCollection myWebHeaderCollection = httpWebRequest.Headers;
|
|
427
|
+ //myWebHeaderCollection.Add("Accept", "*/*");
|
|
428
|
+ if (header != "")
|
|
429
|
+ {
|
|
430
|
+ myWebHeaderCollection.Add("access_token:" + header);
|
|
431
|
+ }
|
|
432
|
+
|
|
433
|
+ HttpWebResponse webRespon = (HttpWebResponse)httpWebRequest.GetResponse();
|
|
434
|
+ Stream webStream = webRespon.GetResponseStream();
|
|
435
|
+ long length = webRespon.ContentLength;
|
|
436
|
+ if (webStream == null)
|
|
437
|
+ {
|
|
438
|
+ return false;
|
|
439
|
+ }
|
|
440
|
+ int lengthint = Convert.ToInt32(length);
|
|
441
|
+ //int num = lengthint / 1024;
|
|
442
|
+ //int count = 0;
|
|
443
|
+ //M_DownloadInfo.totalCount = lengthint / 1024;
|
|
444
|
+ //M_DownloadInfo.downloadCount = 0;
|
|
445
|
+
|
|
446
|
+ Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
|
|
447
|
+ byte[] bArr = new byte[1024];
|
|
448
|
+ int size = webStream.Read(bArr, 0, bArr.Length);
|
|
449
|
+ while (size > 0)
|
|
450
|
+ {
|
|
451
|
+ stream.Write(bArr, 0, size);
|
|
452
|
+ size = webStream.Read(bArr, 0, bArr.Length);
|
|
453
|
+ //M_DownloadInfo.downloadCount = M_DownloadInfo.downloadCount + 1;
|
|
454
|
+ }
|
|
455
|
+ webRespon.Close();
|
|
456
|
+ stream.Close();
|
|
457
|
+ webStream.Close();
|
|
458
|
+ return true;
|
|
459
|
+ }
|
|
460
|
+ catch (Exception ex)
|
|
461
|
+ {
|
|
462
|
+ //LogHelper.WriteErrLog("请求失败:" + ex.Message, ex);
|
|
463
|
+ return false;
|
|
464
|
+ }
|
|
465
|
+ }
|
|
466
|
+
|
|
467
|
+ /// <summary>
|
|
468
|
+ /// 上传文件流
|
|
469
|
+ /// 创建人:赵耀
|
|
470
|
+ /// 创建时间:2020年9月5日
|
|
471
|
+ /// </summary>
|
|
472
|
+ /// <param name="url">URL</param>
|
|
473
|
+ /// <param name="databyte">文件数据流</param>
|
|
474
|
+ /// <param name="filename">文件名</param>
|
|
475
|
+ /// <param name="formFields">参数 可不传</param>
|
|
476
|
+ public static JObject UploadRequestflow(string url, byte[] databyte, string filename, NameValueCollection formFields = null)
|
|
477
|
+ {
|
|
478
|
+ JObject jobResult = null;
|
|
479
|
+ // 时间戳,用做boundary
|
|
480
|
+ string boundary = "-----" + DateTime.Now.Ticks.ToString("x");
|
|
481
|
+ byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
|
482
|
+
|
|
483
|
+ //根据uri创建HttpWebRequest对象
|
|
484
|
+ HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
|
|
485
|
+ httpReq.ContentType = "multipart/form-data; boundary=" + boundary;
|
|
486
|
+ httpReq.Method = "POST";
|
|
487
|
+ httpReq.KeepAlive = true;
|
|
488
|
+ //httpReq.Timeout = 300000;
|
|
489
|
+ //httpReq.AllowWriteStreamBuffering = false; //对发送的数据不使用缓存
|
|
490
|
+ httpReq.Credentials = CredentialCache.DefaultCredentials;
|
|
491
|
+
|
|
492
|
+ try
|
|
493
|
+ {
|
|
494
|
+ Stream postStream = httpReq.GetRequestStream();
|
|
495
|
+ //参数
|
|
496
|
+ const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
|
497
|
+ if (formFields != null)
|
|
498
|
+ {
|
|
499
|
+ foreach (string key in formFields.Keys)
|
|
500
|
+ {
|
|
501
|
+ postStream.Write(boundarybytes, 0, boundarybytes.Length);
|
|
502
|
+ string formitem = string.Format(formdataTemplate, key, formFields[key]);
|
|
503
|
+ byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
|
|
504
|
+ postStream.Write(formitembytes, 0, formitembytes.Length);
|
|
505
|
+ }
|
|
506
|
+ }
|
|
507
|
+ postStream.Write(boundarybytes, 0, boundarybytes.Length);
|
|
508
|
+
|
|
509
|
+ const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
|
|
510
|
+
|
|
511
|
+ //文件头
|
|
512
|
+ string header = string.Format(headerTemplate, "file", filename);
|
|
513
|
+ byte[] headerbytes = Encoding.UTF8.GetBytes(header);
|
|
514
|
+ postStream.Write(headerbytes, 0, headerbytes.Length);
|
|
515
|
+
|
|
516
|
+ //文件流
|
|
517
|
+ postStream.Write(databyte, 0, databyte.Length);
|
|
518
|
+
|
|
519
|
+ //结束边界
|
|
520
|
+ byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
|
521
|
+ postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
|
|
522
|
+ postStream.Close();
|
|
523
|
+
|
|
524
|
+ //获取服务器端的响应
|
|
525
|
+ using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse())
|
|
526
|
+ {
|
|
527
|
+ Stream receiveStream = response.GetResponseStream();
|
|
528
|
+ StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
|
|
529
|
+ string returnValue = readStream.ReadToEnd();
|
|
530
|
+ jobResult = JObject.Parse(returnValue);
|
|
531
|
+ response.Close();
|
|
532
|
+ readStream.Close();
|
|
533
|
+ //MessageBox.Show(returnValue);
|
|
534
|
+ LogHelper.WriteInfoLog("【文件上传】" + returnValue);
|
|
535
|
+ }
|
|
536
|
+ return jobResult;
|
|
537
|
+ }
|
|
538
|
+ catch (Exception ex)
|
|
539
|
+ {
|
|
540
|
+ LogHelper.WriteErrLog("【文件上传】" + ex.Message, ex);
|
|
541
|
+ return null;
|
|
542
|
+ }
|
|
543
|
+ finally
|
|
544
|
+ {
|
|
545
|
+ httpReq = null;
|
|
546
|
+ }
|
|
547
|
+ }
|
211
|
548
|
}
|
212
|
549
|
}
|