Created
December 10, 2018 22:11
-
-
Save ian-beer/54ff92358b045113c0c1b8e3dd8b718e to your computer and use it in GitHub Desktop.
.Net HttpClient helpers
This file contains hidden or 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
private static HttpClient _client = new HttpClient(); | |
private static T HttpGet<T>(string url) | |
{ | |
var response = _client.GetAsync(url).GetAwaiter().GetResult(); | |
if (false == response.IsSuccessStatusCode) | |
{ | |
var responseContent = response.Content.ReadAsStringAsync(); | |
throw new Exception(responseContent.Result); | |
} | |
return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); | |
} | |
private T HttpPost<T>(string url, object content) | |
{ | |
var response = _client.PostAsJsonAsync(url, content).GetAwaiter().GetResult(); | |
if (false == response.IsSuccessStatusCode) | |
{ | |
var responseContent = response.Content.ReadAsStringAsync(); | |
throw new Exception(responseContent.Result); | |
} | |
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); | |
return JsonConvert.DeserializeObject<T>(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment