|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+using System;
|
|
|
2
|
+using System.Collections;
|
|
|
3
|
+using System.Collections.Generic;
|
|
|
4
|
+using System.IO;
|
|
|
5
|
+using System.Net;
|
|
|
6
|
+using System.Net.Http;
|
|
|
7
|
+using System.Net.Security;
|
|
|
8
|
+using System.Security.Cryptography.X509Certificates;
|
|
|
9
|
+using System.Text;
|
|
|
10
|
+
|
|
|
11
|
+namespace System.Utility
|
|
|
12
|
+{
|
|
|
13
|
+ public class HttpMethods
|
|
|
14
|
+ {
|
|
|
15
|
+ #region POST
|
|
|
16
|
+ /// <summary>
|
|
|
17
|
+ /// HTTP POST方式请求数据
|
|
|
18
|
+ /// </summary>
|
|
|
19
|
+ /// <param name="url">URL.</param>
|
|
|
20
|
+ /// <param name="param">POST的数据</param>
|
|
|
21
|
+ /// <returns></returns>
|
|
|
22
|
+ public static string HttpPost(string url, string param = null)
|
|
|
23
|
+ {
|
|
|
24
|
+ HttpWebRequest request;
|
|
|
25
|
+
|
|
|
26
|
+ //如果是发送HTTPS请求
|
|
|
27
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
28
|
+ {
|
|
|
29
|
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
|
|
30
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
31
|
+ request.ProtocolVersion = HttpVersion.Version10;
|
|
|
32
|
+ }
|
|
|
33
|
+ else
|
|
|
34
|
+ {
|
|
|
35
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
36
|
+ }
|
|
|
37
|
+
|
|
|
38
|
+ request.Method = "POST";
|
|
|
39
|
+ request.ContentType = "application/x-www-form-urlencoded";
|
|
|
40
|
+ request.Accept = "*/*";
|
|
|
41
|
+ request.Timeout = 15000;
|
|
|
42
|
+ request.AllowAutoRedirect = false;
|
|
|
43
|
+
|
|
|
44
|
+
|
|
|
45
|
+
|
|
|
46
|
+ StreamWriter requestStream = null;
|
|
|
47
|
+ WebResponse response = null;
|
|
|
48
|
+ string responseStr = null;
|
|
|
49
|
+
|
|
|
50
|
+ try
|
|
|
51
|
+ {
|
|
|
52
|
+ requestStream = new StreamWriter(request.GetRequestStream());
|
|
|
53
|
+ requestStream.Write(param);
|
|
|
54
|
+ requestStream.Close();
|
|
|
55
|
+
|
|
|
56
|
+ response = request.GetResponse();
|
|
|
57
|
+ if (response != null)
|
|
|
58
|
+ {
|
|
|
59
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
60
|
+ responseStr = reader.ReadToEnd();
|
|
|
61
|
+ reader.Close();
|
|
|
62
|
+ }
|
|
|
63
|
+ }
|
|
|
64
|
+ catch (Exception)
|
|
|
65
|
+ {
|
|
|
66
|
+ throw;
|
|
|
67
|
+ }
|
|
|
68
|
+ finally
|
|
|
69
|
+ {
|
|
|
70
|
+ request = null;
|
|
|
71
|
+ requestStream = null;
|
|
|
72
|
+ response = null;
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ return responseStr;
|
|
|
76
|
+ }
|
|
|
77
|
+
|
|
|
78
|
+
|
|
|
79
|
+ private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
|
|
80
|
+ {
|
|
|
81
|
+ return true; //总是接受
|
|
|
82
|
+ }
|
|
|
83
|
+ public static string BuildRequest(string strUrl, Dictionary<string, string> dicPara, string fileName)
|
|
|
84
|
+ {
|
|
|
85
|
+ string contentType = "image/jpeg";
|
|
|
86
|
+ //待请求参数数组
|
|
|
87
|
+ FileStream Pic = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
88
|
+ byte[] PicByte = new byte[Pic.Length];
|
|
|
89
|
+ Pic.Read(PicByte, 0, PicByte.Length);
|
|
|
90
|
+ int lengthFile = PicByte.Length;
|
|
|
91
|
+
|
|
|
92
|
+ //构造请求地址
|
|
|
93
|
+
|
|
|
94
|
+ //设置HttpWebRequest基本信息
|
|
|
95
|
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
|
|
|
96
|
+ //设置请求方式:get、post
|
|
|
97
|
+ request.Method = "POST";
|
|
|
98
|
+ //设置boundaryValue
|
|
|
99
|
+ string boundaryValue = DateTime.Now.Ticks.ToString("x");
|
|
|
100
|
+ string boundary = "--" + boundaryValue;
|
|
|
101
|
+ request.ContentType = "\r\nmultipart/form-data; boundary=" + boundaryValue;
|
|
|
102
|
+ //设置KeepAlive
|
|
|
103
|
+ request.KeepAlive = true;
|
|
|
104
|
+ //设置请求数据,拼接成字符串
|
|
|
105
|
+ StringBuilder sbHtml = new StringBuilder();
|
|
|
106
|
+ foreach (KeyValuePair<string, string> key in dicPara)
|
|
|
107
|
+ {
|
|
|
108
|
+ sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"" + key.Key + "\"\r\n\r\n" + key.Value + "\r\n");
|
|
|
109
|
+ }
|
|
|
110
|
+ sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"");
|
|
|
111
|
+ sbHtml.Append(fileName);
|
|
|
112
|
+ sbHtml.Append("\"\r\nContent-Type: " + contentType + "\r\n\r\n");
|
|
|
113
|
+ string postHeader = sbHtml.ToString();
|
|
|
114
|
+ //将请求数据字符串类型根据编码格式转换成字节流
|
|
|
115
|
+ Encoding code = Encoding.GetEncoding("UTF-8");
|
|
|
116
|
+ byte[] postHeaderBytes = code.GetBytes(postHeader);
|
|
|
117
|
+ byte[] boundayBytes = Encoding.ASCII.GetBytes("\r\n" + boundary + "--\r\n");
|
|
|
118
|
+ //设置长度
|
|
|
119
|
+ long length = postHeaderBytes.Length + lengthFile + boundayBytes.Length;
|
|
|
120
|
+ request.ContentLength = length;
|
|
|
121
|
+
|
|
|
122
|
+ //请求远程HTTP
|
|
|
123
|
+ Stream requestStream = request.GetRequestStream();
|
|
|
124
|
+ Stream myStream = null;
|
|
|
125
|
+ try
|
|
|
126
|
+ {
|
|
|
127
|
+ //发送数据请求服务器
|
|
|
128
|
+ requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
|
|
|
129
|
+ requestStream.Write(PicByte, 0, lengthFile);
|
|
|
130
|
+ requestStream.Write(boundayBytes, 0, boundayBytes.Length);
|
|
|
131
|
+ HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
|
|
|
132
|
+ myStream = HttpWResp.GetResponseStream();
|
|
|
133
|
+ }
|
|
|
134
|
+ catch (WebException e)
|
|
|
135
|
+ {
|
|
|
136
|
+ //LogResult(e.Message);
|
|
|
137
|
+ return "";
|
|
|
138
|
+ }
|
|
|
139
|
+ finally
|
|
|
140
|
+ {
|
|
|
141
|
+ if (requestStream != null)
|
|
|
142
|
+ {
|
|
|
143
|
+ requestStream.Close();
|
|
|
144
|
+ }
|
|
|
145
|
+ }
|
|
|
146
|
+
|
|
|
147
|
+ //读取处理结果
|
|
|
148
|
+ StreamReader reader = new StreamReader(myStream, code);
|
|
|
149
|
+ StringBuilder responseData = new StringBuilder();
|
|
|
150
|
+
|
|
|
151
|
+ String line;
|
|
|
152
|
+ while ((line = reader.ReadLine()) != null)
|
|
|
153
|
+ {
|
|
|
154
|
+ responseData.Append(line);
|
|
|
155
|
+ }
|
|
|
156
|
+ myStream.Close();
|
|
|
157
|
+ Pic.Close();
|
|
|
158
|
+
|
|
|
159
|
+ return responseData.ToString();
|
|
|
160
|
+ }
|
|
|
161
|
+ #endregion
|
|
|
162
|
+
|
|
|
163
|
+ #region Put
|
|
|
164
|
+ /// <summary>
|
|
|
165
|
+ /// HTTP Put方式请求数据.
|
|
|
166
|
+ /// </summary>
|
|
|
167
|
+ /// <param name="url">URL.</param>
|
|
|
168
|
+ /// <returns></returns>
|
|
|
169
|
+ public static string HttpPut(string url, string param = null)
|
|
|
170
|
+ {
|
|
|
171
|
+ HttpWebRequest request;
|
|
|
172
|
+
|
|
|
173
|
+ //如果是发送HTTPS请求
|
|
|
174
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
175
|
+ {
|
|
|
176
|
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
|
|
177
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
178
|
+ request.ProtocolVersion = HttpVersion.Version10;
|
|
|
179
|
+ }
|
|
|
180
|
+ else
|
|
|
181
|
+ {
|
|
|
182
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
183
|
+ }
|
|
|
184
|
+ request.Method = "PUT";
|
|
|
185
|
+ request.ContentType = "application/x-www-form-urlencoded";
|
|
|
186
|
+ request.Accept = "*/*";
|
|
|
187
|
+ request.Timeout = 15000;
|
|
|
188
|
+ request.AllowAutoRedirect = false;
|
|
|
189
|
+
|
|
|
190
|
+ StreamWriter requestStream = null;
|
|
|
191
|
+ WebResponse response = null;
|
|
|
192
|
+ string responseStr = null;
|
|
|
193
|
+
|
|
|
194
|
+ try
|
|
|
195
|
+ {
|
|
|
196
|
+ requestStream = new StreamWriter(request.GetRequestStream());
|
|
|
197
|
+ requestStream.Write(param);
|
|
|
198
|
+ requestStream.Close();
|
|
|
199
|
+
|
|
|
200
|
+ response = request.GetResponse();
|
|
|
201
|
+ if (response != null)
|
|
|
202
|
+ {
|
|
|
203
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
204
|
+ responseStr = reader.ReadToEnd();
|
|
|
205
|
+ reader.Close();
|
|
|
206
|
+ }
|
|
|
207
|
+ }
|
|
|
208
|
+ catch (Exception)
|
|
|
209
|
+ {
|
|
|
210
|
+ throw;
|
|
|
211
|
+ }
|
|
|
212
|
+ finally
|
|
|
213
|
+ {
|
|
|
214
|
+ request = null;
|
|
|
215
|
+ requestStream = null;
|
|
|
216
|
+ response = null;
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ return responseStr;
|
|
|
220
|
+ }
|
|
|
221
|
+ #endregion
|
|
|
222
|
+
|
|
|
223
|
+ #region Delete
|
|
|
224
|
+ /// <summary>
|
|
|
225
|
+ /// HTTP Delete方式请求数据.
|
|
|
226
|
+ /// </summary>
|
|
|
227
|
+ /// <param name="url">URL.</param>
|
|
|
228
|
+ /// <returns></returns>
|
|
|
229
|
+ public static string HttpDelete(string url, string param = null)
|
|
|
230
|
+ {
|
|
|
231
|
+ HttpWebRequest request;
|
|
|
232
|
+
|
|
|
233
|
+ //如果是发送HTTPS请求
|
|
|
234
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
235
|
+ {
|
|
|
236
|
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
|
|
237
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
238
|
+ request.ProtocolVersion = HttpVersion.Version10;
|
|
|
239
|
+ }
|
|
|
240
|
+ else
|
|
|
241
|
+ {
|
|
|
242
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
243
|
+ }
|
|
|
244
|
+ request.Method = "Delete";
|
|
|
245
|
+ request.ContentType = "application/x-www-form-urlencoded";
|
|
|
246
|
+ request.Accept = "*/*";
|
|
|
247
|
+ request.Timeout = 15000;
|
|
|
248
|
+ request.AllowAutoRedirect = false;
|
|
|
249
|
+
|
|
|
250
|
+ StreamWriter requestStream = null;
|
|
|
251
|
+ WebResponse response = null;
|
|
|
252
|
+ string responseStr = null;
|
|
|
253
|
+
|
|
|
254
|
+ try
|
|
|
255
|
+ {
|
|
|
256
|
+ requestStream = new StreamWriter(request.GetRequestStream());
|
|
|
257
|
+ requestStream.Write(param);
|
|
|
258
|
+ requestStream.Close();
|
|
|
259
|
+
|
|
|
260
|
+ response = request.GetResponse();
|
|
|
261
|
+ if (response != null)
|
|
|
262
|
+ {
|
|
|
263
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
264
|
+ responseStr = reader.ReadToEnd();
|
|
|
265
|
+ reader.Close();
|
|
|
266
|
+ }
|
|
|
267
|
+ }
|
|
|
268
|
+ catch (Exception)
|
|
|
269
|
+ {
|
|
|
270
|
+ throw;
|
|
|
271
|
+ }
|
|
|
272
|
+ return responseStr;
|
|
|
273
|
+ }
|
|
|
274
|
+ #endregion
|
|
|
275
|
+
|
|
|
276
|
+ #region Get
|
|
|
277
|
+ /// <summary>
|
|
|
278
|
+ /// HTTP GET方式请求数据.
|
|
|
279
|
+ /// </summary>
|
|
|
280
|
+ /// <param name="url">URL.</param>
|
|
|
281
|
+ /// <returns></returns>
|
|
|
282
|
+ public static string HttpGet(string url, Hashtable headht = null)
|
|
|
283
|
+ {
|
|
|
284
|
+ HttpWebRequest request;
|
|
|
285
|
+
|
|
|
286
|
+ //如果是发送HTTPS请求
|
|
|
287
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
288
|
+ {
|
|
|
289
|
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
|
|
290
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
291
|
+ request.ProtocolVersion = HttpVersion.Version10;
|
|
|
292
|
+ }
|
|
|
293
|
+ else
|
|
|
294
|
+ {
|
|
|
295
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
296
|
+ }
|
|
|
297
|
+ request.Method = "GET";
|
|
|
298
|
+ //request.ContentType = "application/x-www-form-urlencoded";
|
|
|
299
|
+ request.Accept = "*/*";
|
|
|
300
|
+ request.Timeout = 15000;
|
|
|
301
|
+ request.AllowAutoRedirect = false;
|
|
|
302
|
+ WebResponse response = null;
|
|
|
303
|
+ string responseStr = null;
|
|
|
304
|
+ if (headht != null)
|
|
|
305
|
+ {
|
|
|
306
|
+ foreach (DictionaryEntry item in headht)
|
|
|
307
|
+ {
|
|
|
308
|
+ request.Headers.Add(item.Key.ToString(), item.Value.ToString());
|
|
|
309
|
+ }
|
|
|
310
|
+ }
|
|
|
311
|
+
|
|
|
312
|
+ try
|
|
|
313
|
+ {
|
|
|
314
|
+ response = request.GetResponse();
|
|
|
315
|
+
|
|
|
316
|
+ if (response != null)
|
|
|
317
|
+ {
|
|
|
318
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
319
|
+ responseStr = reader.ReadToEnd();
|
|
|
320
|
+ reader.Close();
|
|
|
321
|
+ }
|
|
|
322
|
+ }
|
|
|
323
|
+ catch (Exception)
|
|
|
324
|
+ {
|
|
|
325
|
+ throw;
|
|
|
326
|
+ }
|
|
|
327
|
+ return responseStr;
|
|
|
328
|
+ }
|
|
|
329
|
+ public static string HttpGet(string url, Encoding encodeing, Hashtable headht = null)
|
|
|
330
|
+ {
|
|
|
331
|
+ HttpWebRequest request;
|
|
|
332
|
+
|
|
|
333
|
+ //如果是发送HTTPS请求
|
|
|
334
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
335
|
+ {
|
|
|
336
|
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
|
|
337
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
338
|
+ request.ProtocolVersion = HttpVersion.Version10;
|
|
|
339
|
+ }
|
|
|
340
|
+ else
|
|
|
341
|
+ {
|
|
|
342
|
+ request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
343
|
+ }
|
|
|
344
|
+ request.Method = "GET";
|
|
|
345
|
+ //request.ContentType = "application/x-www-form-urlencoded";
|
|
|
346
|
+ request.Accept = "*/*";
|
|
|
347
|
+ request.Timeout = 15000;
|
|
|
348
|
+ request.AllowAutoRedirect = false;
|
|
|
349
|
+ WebResponse response = null;
|
|
|
350
|
+ string responseStr = null;
|
|
|
351
|
+ if (headht != null)
|
|
|
352
|
+ {
|
|
|
353
|
+ foreach (DictionaryEntry item in headht)
|
|
|
354
|
+ {
|
|
|
355
|
+ request.Headers.Add(item.Key.ToString(), item.Value.ToString());
|
|
|
356
|
+ }
|
|
|
357
|
+ }
|
|
|
358
|
+
|
|
|
359
|
+ try
|
|
|
360
|
+ {
|
|
|
361
|
+ response = request.GetResponse();
|
|
|
362
|
+
|
|
|
363
|
+ if (response != null)
|
|
|
364
|
+ {
|
|
|
365
|
+ StreamReader reader = new StreamReader(response.GetResponseStream(), encodeing);
|
|
|
366
|
+ responseStr = reader.ReadToEnd();
|
|
|
367
|
+ reader.Close();
|
|
|
368
|
+ }
|
|
|
369
|
+ }
|
|
|
370
|
+ catch (Exception)
|
|
|
371
|
+ {
|
|
|
372
|
+ throw;
|
|
|
373
|
+ }
|
|
|
374
|
+ return responseStr;
|
|
|
375
|
+ }
|
|
|
376
|
+ #endregion
|
|
|
377
|
+
|
|
|
378
|
+ #region Post With Pic
|
|
|
379
|
+ private string HttpPost(string url, IDictionary<object, object> param, string filePath)
|
|
|
380
|
+ {
|
|
|
381
|
+ string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
|
|
|
382
|
+ byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
|
|
383
|
+
|
|
|
384
|
+ HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
|
|
|
385
|
+ wr.ContentType = "multipart/form-data; boundary=" + boundary;
|
|
|
386
|
+ wr.Method = "POST";
|
|
|
387
|
+ wr.KeepAlive = true;
|
|
|
388
|
+ wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
|
|
|
389
|
+
|
|
|
390
|
+ Stream rs = wr.GetRequestStream();
|
|
|
391
|
+ string responseStr = null;
|
|
|
392
|
+
|
|
|
393
|
+ string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
|
|
394
|
+ foreach (string key in param.Keys)
|
|
|
395
|
+ {
|
|
|
396
|
+ rs.Write(boundarybytes, 0, boundarybytes.Length);
|
|
|
397
|
+ string formitem = string.Format(formdataTemplate, key, param[key]);
|
|
|
398
|
+ byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
|
|
|
399
|
+ rs.Write(formitembytes, 0, formitembytes.Length);
|
|
|
400
|
+ }
|
|
|
401
|
+ rs.Write(boundarybytes, 0, boundarybytes.Length);
|
|
|
402
|
+
|
|
|
403
|
+ string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
|
|
|
404
|
+ string header = string.Format(headerTemplate, "pic", filePath, "text/plain");
|
|
|
405
|
+ byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
|
|
|
406
|
+ rs.Write(headerbytes, 0, headerbytes.Length);
|
|
|
407
|
+
|
|
|
408
|
+ FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
|
|
409
|
+ byte[] buffer = new byte[4096];
|
|
|
410
|
+ int bytesRead = 0;
|
|
|
411
|
+ while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
|
|
412
|
+ {
|
|
|
413
|
+ rs.Write(buffer, 0, bytesRead);
|
|
|
414
|
+ }
|
|
|
415
|
+ fileStream.Close();
|
|
|
416
|
+
|
|
|
417
|
+ byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
|
|
418
|
+ rs.Write(trailer, 0, trailer.Length);
|
|
|
419
|
+ rs.Close();
|
|
|
420
|
+
|
|
|
421
|
+ WebResponse wresp = null;
|
|
|
422
|
+ try
|
|
|
423
|
+ {
|
|
|
424
|
+ wresp = wr.GetResponse();
|
|
|
425
|
+ Stream stream2 = wresp.GetResponseStream();
|
|
|
426
|
+ StreamReader reader2 = new StreamReader(stream2);
|
|
|
427
|
+ responseStr = reader2.ReadToEnd();
|
|
|
428
|
+ //logger.Debug(string.Format("File uploaded, server response is: {0}", responseStr));
|
|
|
429
|
+ }
|
|
|
430
|
+ catch (Exception ex)
|
|
|
431
|
+ {
|
|
|
432
|
+ //logger.Error("Error uploading file", ex);
|
|
|
433
|
+ if (wresp != null)
|
|
|
434
|
+ {
|
|
|
435
|
+ wresp.Close();
|
|
|
436
|
+ wresp = null;
|
|
|
437
|
+ }
|
|
|
438
|
+ throw;
|
|
|
439
|
+ }
|
|
|
440
|
+ return responseStr;
|
|
|
441
|
+ }
|
|
|
442
|
+ #endregion
|
|
|
443
|
+
|
|
|
444
|
+ #region Post With Pic
|
|
|
445
|
+ /// <summary>
|
|
|
446
|
+ /// HTTP POST方式请求数据(带图片)
|
|
|
447
|
+ /// </summary>
|
|
|
448
|
+ /// <param name="url">URL</param>
|
|
|
449
|
+ /// <param name="param">POST的数据</param>
|
|
|
450
|
+ /// <param name="fileByte">图片</param>
|
|
|
451
|
+ /// <returns></returns>
|
|
|
452
|
+ public static string HttpPost(string url, IDictionary<object, object> param, byte[] fileByte)
|
|
|
453
|
+ {
|
|
|
454
|
+ string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
|
|
|
455
|
+ byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
|
|
456
|
+
|
|
|
457
|
+ HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
|
|
|
458
|
+ wr.ContentType = "multipart/form-data; boundary=" + boundary;
|
|
|
459
|
+ wr.Method = "POST";
|
|
|
460
|
+ wr.KeepAlive = true;
|
|
|
461
|
+ wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
|
|
|
462
|
+
|
|
|
463
|
+ Stream rs = wr.GetRequestStream();
|
|
|
464
|
+ string responseStr = null;
|
|
|
465
|
+
|
|
|
466
|
+ string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
|
|
467
|
+ foreach (string key in param.Keys)
|
|
|
468
|
+ {
|
|
|
469
|
+ rs.Write(boundarybytes, 0, boundarybytes.Length);
|
|
|
470
|
+ string formitem = string.Format(formdataTemplate, key, param[key]);
|
|
|
471
|
+ byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
|
|
|
472
|
+ rs.Write(formitembytes, 0, formitembytes.Length);
|
|
|
473
|
+ }
|
|
|
474
|
+ rs.Write(boundarybytes, 0, boundarybytes.Length);
|
|
|
475
|
+
|
|
|
476
|
+ string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
|
|
|
477
|
+ string header = string.Format(headerTemplate, "pic", fileByte, "text/plain");//image/jpeg
|
|
|
478
|
+ byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
|
|
|
479
|
+ rs.Write(headerbytes, 0, headerbytes.Length);
|
|
|
480
|
+
|
|
|
481
|
+ rs.Write(fileByte, 0, fileByte.Length);
|
|
|
482
|
+
|
|
|
483
|
+ byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
|
|
484
|
+ rs.Write(trailer, 0, trailer.Length);
|
|
|
485
|
+ rs.Close();
|
|
|
486
|
+
|
|
|
487
|
+ WebResponse wresp = null;
|
|
|
488
|
+ try
|
|
|
489
|
+ {
|
|
|
490
|
+ wresp = wr.GetResponse();
|
|
|
491
|
+ Stream stream2 = wresp.GetResponseStream();
|
|
|
492
|
+ StreamReader reader2 = new StreamReader(stream2);
|
|
|
493
|
+ responseStr = reader2.ReadToEnd();
|
|
|
494
|
+ // logger.Error(string.Format("File uploaded, server response is: {0}", responseStr));
|
|
|
495
|
+ }
|
|
|
496
|
+ catch (Exception ex)
|
|
|
497
|
+ {
|
|
|
498
|
+ //logger.Error("Error uploading file", ex);
|
|
|
499
|
+ if (wresp != null)
|
|
|
500
|
+ {
|
|
|
501
|
+ wresp.Close();
|
|
|
502
|
+ wresp = null;
|
|
|
503
|
+ }
|
|
|
504
|
+ throw;
|
|
|
505
|
+ }
|
|
|
506
|
+ return responseStr;
|
|
|
507
|
+ }
|
|
|
508
|
+ #endregion
|
|
|
509
|
+
|
|
|
510
|
+ #region HttpsClient
|
|
|
511
|
+ /// <summary>
|
|
|
512
|
+ /// 创建HttpClient
|
|
|
513
|
+ /// </summary>
|
|
|
514
|
+ /// <returns></returns>
|
|
|
515
|
+ public static HttpClient CreateHttpClient(string url)
|
|
|
516
|
+ {
|
|
|
517
|
+ HttpClient httpclient;
|
|
|
518
|
+ //如果是发送HTTPS请求
|
|
|
519
|
+ if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
|
520
|
+ {
|
|
|
521
|
+ ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
|
|
|
522
|
+ httpclient = new HttpClient();
|
|
|
523
|
+ }
|
|
|
524
|
+ else
|
|
|
525
|
+ {
|
|
|
526
|
+ httpclient = new HttpClient();
|
|
|
527
|
+ }
|
|
|
528
|
+ return httpclient;
|
|
|
529
|
+ }
|
|
|
530
|
+ #endregion
|
|
|
531
|
+ }
|
|
|
532
|
+}
|