wasSharp – Blame information for rev 39

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