wasSharp – Blame information for rev 57
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
24 | office | 9 | using System.IO; |
10 | office | 10 | using System.Net; |
11 | using System.Net.Http; |
||
12 | using System.Net.Http.Headers; |
||
13 | using System.Text; |
||
14 | using System.Threading.Tasks; |
||
55 | office | 15 | using wasSharp.Languages; |
10 | office | 16 | |
17 | namespace wasSharp.Web |
||
18 | { |
||
19 | /////////////////////////////////////////////////////////////////////////// |
||
20 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
21 | /////////////////////////////////////////////////////////////////////////// |
||
22 | // <summary>A portable HTTP client.</summar> |
||
14 | office | 23 | public class wasHTTPClient : IDisposable |
10 | office | 24 | { |
57 | office | 25 | private readonly HttpClient HTTPClient; |
10 | office | 26 | private readonly string MediaType; |
27 | |||
28 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
||
29 | uint timeout) : this(userAgent, cookieContainer, mediaType, null, null, timeout) |
||
30 | { |
||
31 | } |
||
32 | |||
33 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout) |
||
34 | : this(userAgent, new CookieContainer(), mediaType, null, null, timeout) |
||
35 | { |
||
36 | } |
||
37 | |||
48 | office | 38 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType) |
39 | : this(userAgent, new CookieContainer(), mediaType, null, null, 60000) |
||
40 | { |
||
41 | } |
||
42 | |||
54 | office | 43 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType) |
44 | : this(userAgent, new CookieContainer(), mediaType, null, null, 60000) |
||
45 | { |
||
46 | } |
||
47 | |||
10 | office | 48 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
49 | AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout) |
||
50 | { |
||
51 | var HTTPClientHandler = new HttpClientHandler |
||
52 | { |
||
53 | CookieContainer = cookieContainer, |
||
54 | UseCookies = true |
||
55 | }; |
||
56 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
||
57 | { |
||
58 | HTTPClientHandler.AllowAutoRedirect = true; |
||
59 | } |
||
60 | if (HTTPClientHandler.SupportsProxy) |
||
61 | { |
||
62 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
||
63 | HTTPClientHandler.UseProxy = true; |
||
64 | } |
||
65 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
||
66 | { |
||
67 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
||
68 | } |
||
69 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
||
70 | |||
71 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
||
72 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
||
73 | if (authentication != null) |
||
74 | { |
||
75 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
||
76 | } |
||
17 | office | 77 | // Add some standard headers: |
78 | // Accept - for socially acceptable security of mod_sec |
||
79 | switch (headers != null) |
||
10 | office | 80 | { |
17 | office | 81 | case false: |
27 | office | 82 | headers = new Dictionary<string, string> { { "Accept", @"*/*" } }; |
17 | office | 83 | break; |
27 | office | 84 | |
17 | office | 85 | default: |
86 | if (!headers.ContainsKey("Accept")) |
||
87 | { |
||
88 | headers.Add("Accept", @"*/*"); |
||
89 | } |
||
90 | break; |
||
10 | office | 91 | } |
17 | office | 92 | foreach (var header in headers) |
93 | { |
||
94 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
||
95 | } |
||
10 | office | 96 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
97 | MediaType = mediaType; |
||
98 | } |
||
99 | |||
100 | /////////////////////////////////////////////////////////////////////////// |
||
101 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
102 | /////////////////////////////////////////////////////////////////////////// |
||
103 | /// <summary> |
||
56 | office | 104 | /// Sends a PUT request to an URL with binary data. |
10 | office | 105 | /// </summary> |
106 | /// <param name="URL">the url to send the message to</param> |
||
107 | /// <param name="data">key-value pairs to send</param> |
||
108 | /// <param name="headers">headers to send with the request</param> |
||
109 | public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers) |
||
110 | { |
||
111 | try |
||
112 | { |
||
113 | using (var content = new ByteArrayContent(data)) |
||
114 | { |
||
115 | using ( |
||
116 | var request = new HttpRequestMessage |
||
117 | { |
||
118 | RequestUri = new Uri(URL), |
||
119 | Method = HttpMethod.Put, |
||
120 | Content = content |
||
121 | }) |
||
122 | { |
||
123 | foreach (var header in headers) |
||
124 | { |
||
125 | request.Headers.Add(header.Key, header.Value); |
||
126 | } |
||
17 | office | 127 | // Add some standard headers: |
128 | // Accept - for socially acceptable security of mod_sec |
||
129 | if (!headers.ContainsKey("Accept")) |
||
130 | { |
||
131 | headers.Add("Accept", @"*/*"); |
||
132 | } |
||
10 | office | 133 | using (var response = await HTTPClient.SendAsync(request)) |
134 | { |
||
135 | return response.IsSuccessStatusCode |
||
136 | ? await response.Content.ReadAsByteArrayAsync() |
||
137 | : null; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | catch (Exception) |
||
143 | { |
||
144 | return null; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /////////////////////////////////////////////////////////////////////////// |
||
149 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
150 | /////////////////////////////////////////////////////////////////////////// |
||
151 | /// <summary> |
||
56 | office | 152 | /// Sends a PUT request to an URL with text. |
10 | office | 153 | /// </summary> |
154 | /// <param name="URL">the url to send the message to</param> |
||
155 | /// <param name="data">key-value pairs to send</param> |
||
156 | /// <param name="headers">headers to send with the request</param> |
||
157 | public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers) |
||
158 | { |
||
159 | try |
||
160 | { |
||
161 | using (var content = |
||
162 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
163 | { |
||
164 | using ( |
||
165 | var request = new HttpRequestMessage |
||
166 | { |
||
167 | RequestUri = new Uri(URL), |
||
168 | Method = HttpMethod.Put, |
||
169 | Content = content |
||
170 | }) |
||
171 | { |
||
172 | foreach (var header in headers) |
||
173 | { |
||
174 | request.Headers.Add(header.Key, header.Value); |
||
175 | } |
||
17 | office | 176 | // Add some standard headers: |
177 | // Accept - for socially acceptable security of mod_sec |
||
178 | if (!headers.ContainsKey("Accept")) |
||
179 | { |
||
180 | headers.Add("Accept", @"*/*"); |
||
181 | } |
||
10 | office | 182 | using (var response = await HTTPClient.SendAsync(request)) |
183 | { |
||
184 | return response.IsSuccessStatusCode |
||
185 | ? await response.Content.ReadAsByteArrayAsync() |
||
186 | : null; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | catch (Exception) |
||
192 | { |
||
193 | return null; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /////////////////////////////////////////////////////////////////////////// |
||
198 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
199 | /////////////////////////////////////////////////////////////////////////// |
||
200 | /// <summary> |
||
56 | office | 201 | /// Sends a PUT request to an URL with binary data. |
10 | office | 202 | /// </summary> |
203 | /// <param name="URL">the url to send the message to</param> |
||
204 | /// <param name="data">key-value pairs to send</param> |
||
205 | public async Task<byte[]> PUT(string URL, byte[] data) |
||
206 | { |
||
207 | try |
||
208 | { |
||
209 | using (var content = new ByteArrayContent(data)) |
||
210 | { |
||
211 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
212 | { |
||
213 | return response.IsSuccessStatusCode |
||
214 | ? await response.Content.ReadAsByteArrayAsync() |
||
215 | : null; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | catch (Exception) |
||
220 | { |
||
221 | return null; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /////////////////////////////////////////////////////////////////////////// |
||
226 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
227 | /////////////////////////////////////////////////////////////////////////// |
||
228 | /// <summary> |
||
56 | office | 229 | /// Sends a PUT request to an URL with stream content data. |
24 | office | 230 | /// </summary> |
231 | /// <param name="URL">the url to send the message to</param> |
||
232 | /// <param name="data">key-value pairs to send</param> |
||
233 | public async Task<byte[]> PUT(string URL, Stream data) |
||
234 | { |
||
235 | try |
||
236 | { |
||
237 | using (var streamContenet = new StreamContent(data)) |
||
238 | { |
||
239 | using (var response = await HTTPClient.PutAsync(URL, streamContenet)) |
||
240 | { |
||
241 | return response.IsSuccessStatusCode |
||
242 | ? await response.Content.ReadAsByteArrayAsync() |
||
243 | : null; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | catch (Exception) |
||
248 | { |
||
249 | return null; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /////////////////////////////////////////////////////////////////////////// |
||
254 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
255 | /////////////////////////////////////////////////////////////////////////// |
||
256 | /// <summary> |
||
56 | office | 257 | /// Sends a DELETE request to an URL with stream content data. |
24 | office | 258 | /// </summary> |
259 | /// <param name="URL">the url to send the message to</param> |
||
260 | /// <param name="data">key-value pairs to send</param> |
||
261 | public async Task<byte[]> DELETE(string URL, Stream data) |
||
262 | { |
||
263 | try |
||
264 | { |
||
265 | using (var streamContenet = new StreamContent(data)) |
||
266 | { |
||
267 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
||
268 | { |
||
269 | Content = streamContenet |
||
270 | }) |
||
271 | { |
||
272 | using (var response = await HTTPClient.SendAsync(request)) |
||
273 | { |
||
274 | return response.IsSuccessStatusCode |
||
275 | ? await response.Content.ReadAsByteArrayAsync() |
||
276 | : null; |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | catch (Exception) |
||
282 | { |
||
283 | return null; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | /////////////////////////////////////////////////////////////////////////// |
||
288 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
289 | /////////////////////////////////////////////////////////////////////////// |
||
290 | /// <summary> |
||
56 | office | 291 | /// Sends a PUT request to an URL with text. |
10 | office | 292 | /// </summary> |
293 | /// <param name="URL">the url to send the message to</param> |
||
294 | /// <param name="data">key-value pairs to send</param> |
||
295 | public async Task<byte[]> PUT(string URL, string data) |
||
296 | { |
||
297 | try |
||
298 | { |
||
299 | using (var content = |
||
300 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
301 | { |
||
302 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
303 | { |
||
304 | return response.IsSuccessStatusCode |
||
305 | ? await response.Content.ReadAsByteArrayAsync() |
||
306 | : null; |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | catch (Exception) |
||
311 | { |
||
312 | return null; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /////////////////////////////////////////////////////////////////////////// |
||
317 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
318 | /////////////////////////////////////////////////////////////////////////// |
||
319 | /// <summary> |
||
56 | office | 320 | /// Sends a PUT request to an URL with text. |
24 | office | 321 | /// </summary> |
322 | /// <param name="URL">the url to send the message to</param> |
||
323 | /// <param name="data">key-value pairs to send</param> |
||
324 | public async Task<byte[]> DELETE(string URL, string data) |
||
325 | { |
||
326 | try |
||
327 | { |
||
328 | using (var content = |
||
329 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
330 | { |
||
331 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
||
332 | { |
||
333 | Content = content |
||
334 | }) |
||
335 | { |
||
336 | using (var response = await HTTPClient.SendAsync(request)) |
||
337 | { |
||
338 | return response.IsSuccessStatusCode |
||
339 | ? await response.Content.ReadAsByteArrayAsync() |
||
340 | : null; |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | catch (Exception) |
||
346 | { |
||
347 | return null; |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /////////////////////////////////////////////////////////////////////////// |
||
352 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
353 | /////////////////////////////////////////////////////////////////////////// |
||
354 | /// <summary> |
||
56 | office | 355 | /// Sends a POST request to an URL with set key-value pairs. |
10 | office | 356 | /// </summary> |
357 | /// <param name="URL">the url to send the message to</param> |
||
358 | /// <param name="message">key-value pairs to send</param> |
||
359 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
||
360 | { |
||
361 | try |
||
362 | { |
||
363 | using (var content = |
||
364 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
365 | { |
||
366 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
367 | { |
||
368 | return response.IsSuccessStatusCode |
||
369 | ? await response.Content.ReadAsByteArrayAsync() |
||
370 | : null; |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | catch (Exception) |
||
375 | { |
||
376 | return null; |
||
377 | } |
||
378 | } |
||
379 | |||
380 | /////////////////////////////////////////////////////////////////////////// |
||
39 | office | 381 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
382 | /////////////////////////////////////////////////////////////////////////// |
||
383 | /// <summary> |
||
56 | office | 384 | /// Sends a POST request to an URL with set message. |
39 | office | 385 | /// </summary> |
386 | /// <param name="URL">the url to send the message to</param> |
||
387 | /// <param name="message">the message to send</param> |
||
388 | public async Task<byte[]> POST(string URL, string message) |
||
389 | { |
||
390 | try |
||
391 | { |
||
392 | using (var content = |
||
393 | new StringContent(message, Encoding.UTF8, MediaType)) |
||
394 | { |
||
395 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
396 | { |
||
397 | return response.IsSuccessStatusCode |
||
398 | ? await response.Content.ReadAsByteArrayAsync() |
||
399 | : null; |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | catch (Exception) |
||
404 | { |
||
405 | return null; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | /////////////////////////////////////////////////////////////////////////// |
||
10 | office | 410 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
411 | /////////////////////////////////////////////////////////////////////////// |
||
412 | /// <summary> |
||
56 | office | 413 | /// Sends a POST request to an URL with set key-value pairs. |
24 | office | 414 | /// </summary> |
415 | /// <param name="URL">the url to send the message to</param> |
||
416 | /// <param name="message">key-value pairs to send</param> |
||
417 | public async Task<byte[]> POST(string URL, IEnumerable<KeyValuePair<string, string>> message) |
||
418 | { |
||
419 | try |
||
420 | { |
||
421 | using (var content = |
||
422 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
423 | { |
||
424 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
425 | { |
||
426 | return response.IsSuccessStatusCode |
||
427 | ? await response.Content.ReadAsByteArrayAsync() |
||
428 | : null; |
||
429 | } |
||
430 | } |
||
431 | } |
||
432 | catch (Exception) |
||
433 | { |
||
434 | return null; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | /////////////////////////////////////////////////////////////////////////// |
||
439 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
440 | /////////////////////////////////////////////////////////////////////////// |
||
441 | /// <summary> |
||
56 | office | 442 | /// Asynchronously sends a POST request to an URL with set key-value pairs. |
10 | office | 443 | /// </summary> |
444 | /// <param name="URL">the url to send the message to</param> |
||
445 | /// <param name="message">key-value pairs to send</param> |
||
56 | office | 446 | public async Task<byte[]> POST(string URL, IEnumerable<KeyValuePair<Task<string>, Task<string>>> message) |
447 | { |
||
448 | try |
||
449 | { |
||
450 | using (var content = |
||
451 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
452 | { |
||
453 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
454 | { |
||
455 | return response.IsSuccessStatusCode |
||
456 | ? await response.Content.ReadAsByteArrayAsync() |
||
457 | : null; |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 | catch (Exception) |
||
462 | { |
||
463 | return null; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | /////////////////////////////////////////////////////////////////////////// |
||
468 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
469 | /////////////////////////////////////////////////////////////////////////// |
||
470 | /// <summary> |
||
471 | /// Sends a GET request to an URL with set key-value pairs. |
||
472 | /// </summary> |
||
473 | /// <param name="URL">the url to send the message to</param> |
||
474 | /// <param name="message">key-value pairs to send</param> |
||
10 | office | 475 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
476 | { |
||
477 | try |
||
478 | { |
||
479 | using (var response = |
||
34 | office | 480 | await HTTPClient.GetAsync($"{URL}?{KeyValue.Encode(message)}")) |
10 | office | 481 | { |
34 | office | 482 | return response.IsSuccessStatusCode ? |
483 | await response.Content.ReadAsByteArrayAsync() : null; |
||
10 | office | 484 | } |
485 | } |
||
486 | catch (Exception) |
||
487 | { |
||
488 | return null; |
||
489 | } |
||
490 | } |
||
14 | office | 491 | |
492 | /////////////////////////////////////////////////////////////////////////// |
||
493 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
494 | /////////////////////////////////////////////////////////////////////////// |
||
495 | /// <summary> |
||
56 | office | 496 | /// Sends a GET request to an URL with set key-value pairs. |
14 | office | 497 | /// </summary> |
498 | /// <param name="URL">the url to send the message to</param> |
||
499 | public async Task<byte[]> GET(string URL) |
||
500 | { |
||
501 | try |
||
502 | { |
||
503 | using (var response = await HTTPClient.GetAsync(URL)) |
||
504 | { |
||
34 | office | 505 | return response.IsSuccessStatusCode ? |
506 | await response.Content.ReadAsByteArrayAsync() : null; |
||
14 | office | 507 | } |
508 | } |
||
509 | catch (Exception) |
||
510 | { |
||
511 | return null; |
||
512 | } |
||
54 | office | 513 | } |
514 | |||
56 | office | 515 | public void Dispose() |
516 | { |
||
517 | ((IDisposable)HTTPClient).Dispose(); |
||
518 | } |
||
10 | office | 519 | } |
27 | office | 520 | } |