public async Task<HttpResult> PostAsync(string uri, string content, Dictionary<string, string> headers = null, string contentType = "application/json") { HttpResult result = new HttpResult(); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); if (headers != null) { foreach (KeyValuePair<string, string> header in headers) { request.Headers.Add(header.Key, header.Value); } } if (!string.IsNullOrWhiteSpace(content)) { request.Content = new StringContent(content, Encoding.UTF8, contentType); } using (HttpResponseMessage response = await Client.SendAsync(request)) { result.StatusCode = response.StatusCode; result.Content = await response.Content.ReadAsStringAsync(); } } catch (Exception e) { result.HasError = true; result.InnerException = e; } return result; }
public async Task<HttpResult> GetAsync(string uri, Dictionary<string, string> headers = null) { HttpResult result = new HttpResult(); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); if (headers != null) { foreach (KeyValuePair<string, string> header in headers) { request.Headers.Add(header.Key, header.Value); } } using (HttpResponseMessage response = await Client.SendAsync(request)) { result.StatusCode = response.StatusCode; result.Content = await response.Content.ReadAsStringAsync(); } } catch (Exception e) { result.HasError = true; result.InnerException = e; } return result; }