Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. protected async Task<Tuple<HttpStatusCode, TOutput>> MakeRequest<TInput, TOutput>(string baseAdrress, string apiUrl, TInput data)
  2. {
  3. var contentData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
  4.  
  5. var client = new HttpClient();
  6. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  7. client.BaseAddress = new Uri(baseAdrress);
  8.  
  9. var response = await client.SendAsync(new HttpRequestMessage
  10. {
  11. RequestUri = new Uri(apiUrl, UriKind.Relative),
  12. Content = contentData,
  13. Method = HttpMethod.Post
  14. });
  15.  
  16. if (response.StatusCode == HttpStatusCode.InternalServerError)
  17. {
  18. return Tuple.Create(HttpStatusCode.InternalServerError, default(TOutput));
  19. }
  20.  
  21. var stringData = await response.Content.ReadAsStringAsync();
  22. var jsonData = JsonConvert.DeserializeObject<TOutput>(stringData);
  23.  
  24. return Tuple.Create(HttpStatusCode.OK, jsonData);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement