/// <summary> /// Process Http Request using instance of HttpClient. /// </summary> /// <param name="request">instance of <see cref="HttpHelperRequest"/></param> /// <param name="cancellationToken">instance of <see cref="CancellationToken"/></param> /// <returns>Instane of <see cref="HttpHelperResponse"/></returns> public async Task <HttpHelperResponse> SendRequestAsync(HttpHelperRequest request, CancellationToken cancellationToken = default(CancellationToken)) { await _semaphore.WaitAsync().ConfigureAwait(false); HttpClient client = null; try { var httpRequestMessage = request.ToHttpRequestMessage(); client = GetHttpClientInstance(); var response = await client.SendRequestAsync(httpRequestMessage).AsTask(cancellationToken).ConfigureAwait(false); FixInvalidCharset(response); return(new HttpHelperResponse(response)); } finally { // Add the HttpClient instance back to the queue. if (client != null) { _httpClientQueue.Enqueue(client); } _semaphore.Release(); } }
/// <summary> /// Process Http Request using instance of HttpClient. /// </summary> /// <param name="request">instance of <see cref="HttpHelperRequest"/></param> /// <returns>Instane of <see cref="HttpHelperResponse"/></returns> public async Task <HttpHelperResponse> SendRequestAsync(HttpHelperRequest request) { var httpRequestMessage = request.ToHttpRequestMessage(); var response = await _httpClient.SendRequestAsync(httpRequestMessage).AsTask().ConfigureAwait(false); FixInvalidCharset(response); return(new HttpHelperResponse(response)); }
/// <summary> /// Process Http Request using instance of HttpClient. /// </summary> /// <param name="request">instance of <see cref="HttpHelperRequest"/></param> /// <param name="cancellationToken">instance of <see cref="CancellationToken"/></param> /// <returns>Instane of <see cref="HttpHelperResponse"/></returns> public async Task <HttpHelperResponse> GetInputStreamAsync(HttpHelperRequest request, CancellationToken cancellationToken = default(CancellationToken)) { await _semaphore.WaitAsync().ConfigureAwait(false); HttpClient client = null; try { var httpRequestMessage = request.ToHttpRequestMessage(); client = GetHttpClientInstance(); foreach (var header in request.Headers) { client.DefaultRequestHeaders[header.Key] = header.Value; } var response = await client.GetInputStreamAsync(httpRequestMessage.RequestUri).AsTask(cancellationToken).ConfigureAwait(false); return(new HttpHelperResponse(new HttpResponseMessage { Content = new HttpStreamContent(response) })); } finally { // Add the HttpClient instance back to the queue. if (client != null) { // Clean up default request headers client.DefaultRequestHeaders.Clear(); _httpClientQueue.Enqueue(client); } _semaphore.Release(); } }