wasSharp – Diff between revs 18 and 24
?pathlinks?
Rev 18 | Rev 24 | |||
---|---|---|---|---|
Line 4... | Line 4... | |||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
Line 6... | Line 6... | |||
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.Net; |
10 | using System.Net; |
|
10 | using System.Net.Http; |
11 | using System.Net.Http; |
|
11 | using System.Net.Http.Headers; |
12 | using System.Net.Http.Headers; |
|
12 | using System.Text; |
13 | using System.Text; |
|
13 | using System.Threading.Tasks; |
- | ||
Line 14... | Line 14... | |||
14 | using wasSharp.Collections.Utilities; |
14 | using System.Threading.Tasks; |
|
15 | |
15 | |
|
16 | namespace wasSharp.Web |
16 | namespace wasSharp.Web |
|
17 | { |
17 | { |
|
Line 83... | Line 83... | |||
83 | } |
83 | } |
|
84 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
84 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
|
85 | MediaType = mediaType; |
85 | MediaType = mediaType; |
|
86 | } |
86 | } |
|
Line -... | Line 87... | |||
- | 87 | |
||
- | 88 | public void Dispose() |
||
- | 89 | { |
||
- | 90 | HTTPClient?.Dispose(); |
||
- | 91 | } |
||
87 | |
92 | |
|
88 | /////////////////////////////////////////////////////////////////////////// |
93 | /////////////////////////////////////////////////////////////////////////// |
|
89 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
94 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
90 | /////////////////////////////////////////////////////////////////////////// |
95 | /////////////////////////////////////////////////////////////////////////// |
|
91 | /// <summary> |
96 | /// <summary> |
|
Line 212... | Line 217... | |||
212 | |
217 | |
|
213 | /////////////////////////////////////////////////////////////////////////// |
218 | /////////////////////////////////////////////////////////////////////////// |
|
214 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
219 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
215 | /////////////////////////////////////////////////////////////////////////// |
220 | /////////////////////////////////////////////////////////////////////////// |
|
- | 221 | /// <summary> |
||
- | 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 | /////////////////////////////////////////////////////////////////////////// |
||
216 | /// <summary> |
283 | /// <summary> |
|
217 | /// Sends a PUT request to an URL with text. |
284 | /// Sends a PUT request to an URL with text. |
|
218 | /// </summary> |
285 | /// </summary> |
|
219 | /// <param name="URL">the url to send the message to</param> |
286 | /// <param name="URL">the url to send the message to</param> |
|
220 | /// <param name="data">key-value pairs to send</param> |
287 | /// <param name="data">key-value pairs to send</param> |
|
Line 241... | Line 308... | |||
241 | |
308 | |
|
242 | /////////////////////////////////////////////////////////////////////////// |
309 | /////////////////////////////////////////////////////////////////////////// |
|
243 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
310 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
244 | /////////////////////////////////////////////////////////////////////////// |
311 | /////////////////////////////////////////////////////////////////////////// |
|
- | 312 | /// <summary> |
||
- | 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 | /////////////////////////////////////////////////////////////////////////// |
||
245 | /// <summary> |
347 | /// <summary> |
|
246 | /// Sends a POST request to an URL with set key-value pairs. |
348 | /// Sends a POST request to an URL with set key-value pairs. |
|
247 | /// </summary> |
349 | /// </summary> |
|
248 | /// <param name="URL">the url to send the message to</param> |
350 | /// <param name="URL">the url to send the message to</param> |
|
249 | /// <param name="message">key-value pairs to send</param> |
351 | /// <param name="message">key-value pairs to send</param> |
|
Line 270... | Line 372... | |||
270 | |
372 | |
|
271 | /////////////////////////////////////////////////////////////////////////// |
373 | /////////////////////////////////////////////////////////////////////////// |
|
272 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
374 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
273 | /////////////////////////////////////////////////////////////////////////// |
375 | /////////////////////////////////////////////////////////////////////////// |
|
- | 376 | /// <summary> |
||
- | 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 | /////////////////////////////////////////////////////////////////////////// |
||
274 | /// <summary> |
405 | /// <summary> |
|
275 | /// Sends a GET request to an URL with set key-value pairs. |
406 | /// Sends a GET request to an URL with set key-value pairs. |
|
276 | /// </summary> |
407 | /// </summary> |
|
277 | /// <param name="URL">the url to send the message to</param> |
408 | /// <param name="URL">the url to send the message to</param> |
|
278 | /// <param name="message">key-value pairs to send</param> |
409 | /// <param name="message">key-value pairs to send</param> |
|
Line 311... | Line 442... | |||
311 | catch (Exception) |
442 | catch (Exception) |
|
312 | { |
443 | { |
|
313 | return null; |
444 | return null; |
|
314 | } |
445 | } |
|
315 | } |
446 | } |
|
316 | |
- | ||
317 | public void Dispose() |
- | ||
318 | { |
- | ||
319 | HTTPClient?.Dispose(); |
- | ||
320 | } |
- | ||
321 | } |
447 | } |
|
322 | } |
448 | } |
|
323 | |
449 | |