public async Task <NetworkResponse <T> > DeleteDataAsync <T>(Uri url, string data, string contentType, ICredentials credentials, Func <string, T> convertAction, CustomHeader customHeader = null) where T : class { return(await SendDataAsync(url, data, contentType, HttpMethod.Delete, credentials, convertAction, customHeader : customHeader)); }
private static async Task <NetworkResponse <T> > SendDataAsync <T>(Uri url, string data, string contentType, HttpMethod httpMethod, ICredentials credentials, Func <string, T> convertAction, string acceptContent = MediaContentTypes.Json, CustomHeader customHeader = null) where T : class { try { using (var handler = new HttpClientHandler { Credentials = credentials }) using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptContent)); if (customHeader != null) { client.DefaultRequestHeaders.Add(customHeader.Name, customHeader.Value); } var requestMessage = new HttpRequestMessage(httpMethod, url); if (data != null) { var requestContent = new StringContent(data, System.Text.Encoding.UTF8, contentType); requestMessage.Content = requestContent; } var response = await client.SendAsync(requestMessage); var reponseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var contentData = convertAction(reponseContent); return(new NetworkResponse <T>(contentData)); } return(new NetworkResponse <T>(new[] { reponseContent })); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <NetworkResponse <T> > PutDataAsync <T>(Uri url, string data, string contentType, ICredentials credentials, Func <string, T> convertAction, string acceptContent = MediaContentTypes.Json, CustomHeader customHeader = null) where T : class { return(await SendDataAsync(url, data, contentType, HttpMethod.Put, credentials, convertAction, acceptContent)); }