This repository was archived by the owner on Aug 31, 2023. It is now read-only.
forked from Rambalac/AmazonCloudDriveApi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpWebRequestExtensions.cs
More file actions
59 lines (55 loc) · 2.1 KB
/
Copy pathHttpWebRequestExtensions.cs
File metadata and controls
59 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Azi.Tools
{
/// <summary>
/// Helper methods to work with Http protocol
/// </summary>
public static class HttpWebRequestExtensions
{
private static readonly HttpStatusCode[] successStatusCodes = { HttpStatusCode.OK, HttpStatusCode.Created, HttpStatusCode.PartialContent };
internal static bool IsSuccessStatusCode(this HttpWebResponse response) => successStatusCodes.Contains(response.StatusCode);
/// <summary>
/// Returns response as string.
/// </summary>
/// <param name="response">Response to read.</param>
/// <returns>String of response.</returns>
public static async Task<string> ReadAsStringAsync(this HttpWebResponse response)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Returns object as parsed JSON from response.
/// </summary>
/// <typeparam name="T">Type of object to parse</typeparam>
/// <param name="response">Response to parse</param>
/// <returns>Parsed object</returns>
public static async Task<T> ReadAsAsync<T>(this HttpWebResponse response)
{
var text = await response.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<T>(text);
}
/// <summary>
/// Returns ContentRange from headers.
/// </summary>
/// <param name="headers">Headers collection</param>
/// <returns>ContentRange object</returns>
public static ContentRangeHeaderValue GetContentRange(this WebHeaderCollection headers)
{
return ContentRangeHeaderValue.Parse(headers["Content-Range"]);
}
}
}