wasSharp – Blame information for rev 54

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