wasSharp – Diff between revs 54 and 55

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