コード例 #1
0
        /// <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();
            }
        }
コード例 #2
0
        /// <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();
            }
        }