-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathStandardHttpClient.cs
38 lines (31 loc) · 1.1 KB
/
StandardHttpClient.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
36
37
38
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Http
{
public class StandardHttpClient : IHttpClient
{
private static readonly HttpClient Client = new HttpClient();
public async Task<string> GetStringAsync(string uri)
{
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
var response = await Client.SendAsync(requestMessage);
return await response.Content.ReadAsStringAsync();
}
public async Task<HttpResponseMessage> PostAsync<T>(string uri, T item)
{
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri)
{
Content = new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8,"application/json")
};
var response = await Client.SendAsync(requestMessage);
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
throw new HttpRequestException();
}
return response;
}
}
}