wasSharp – Blame information for rev 24

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