wasSharp – Blame information for rev 27

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