private static System.Net.Http.HttpMethod ConvertToMethod(HttpMethod method) { switch (method) { case HttpMethod.GET: return(System.Net.Http.HttpMethod.Get); case HttpMethod.PUT: return(System.Net.Http.HttpMethod.Put); case HttpMethod.HEAD: return(System.Net.Http.HttpMethod.Head); case HttpMethod.DELETE: return(System.Net.Http.HttpMethod.Delete); case HttpMethod.POST: return(System.Net.Http.HttpMethod.Post); default: throw new ArgumentOutOfRangeException(nameof(method), method, null); } }
public async Task <(int statusCode, IDictionary <string, string> headers, Stream responseStream)> SendRequestAsync(HttpMethod method, string url, IReadOnlyDictionary <string, string> headers, Stream dataStream, CancellationToken cancellationToken = default) { HttpResponseMessage httpResponse; using (HttpRequestMessage httpRequest = new HttpRequestMessage(ConvertToMethod(method), url)) { if (dataStream != null) { httpRequest.Content = new StreamContent(dataStream); } //Map all the headers to the HTTP request headers. We have to do this after setting the content as some headers are related to content foreach (KeyValuePair <string, string> item in headers) { httpRequest.AddHeader(item.Key, item.Value); } _logger.LogTrace("Sending HTTP request"); httpResponse = await _client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); } _logger.LogDebug("Got an {status} response with {Code}", httpResponse.IsSuccessStatusCode ? "successful" : "unsuccessful", httpResponse.StatusCode); IDictionary <string, string> responseHeaders = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (KeyValuePair <string, IEnumerable <string> > header in httpResponse.Headers) { responseHeaders.Add(header.Key, header.Value.First()); } if (httpResponse.Content != null) { foreach (KeyValuePair <string, IEnumerable <string> > header in httpResponse.Content.Headers) { responseHeaders.Add(header.Key, header.Value.First()); } } return((int)httpResponse.StatusCode, responseHeaders, await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false)); }