Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ASPNETCoreDockerMicroservices/Foundation/Http/StandardHttpClient.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (31 sloc)
1.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
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; | |
} | |
} | |
} |