-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get.cs
35 lines (29 loc) · 1.26 KB
/
Get.cs
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
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace HTTP
{
public static class Get<T>
{
public static async Task<T> GetJsonStreamAsync(Uri Url, string authorizationScheme, string authorizationToken, CancellationToken cancellationToken, TimeSpan httpTimeout)
{
var request = new HttpRequestMessage(HttpMethod.Get, Url);
using (var client = new HttpClient { Timeout = httpTimeout })
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationScheme, authorizationToken);
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
var stream = await response.Content.ReadAsStreamAsync();
if (response.IsSuccessStatusCode)
{
return ClientHelpers.DeserializeJsonFromStream<T>(stream);
}
var content = await ClientHelpers.StreamToStringAsync(stream);
throw new Exception($"{ (int)response.StatusCode } { content}");
}
}
}
}
}