/// <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));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Get the response stream returned by a HTTP get request and save it to a local file.
 /// </summary>
 /// <param name="uri">Uri to request.</param>
 /// <param name="targetFile">StorageFile to save the stream to.</param>
 /// <returns>True if success.</returns>
 public static async Task GetHttpStreamToStorageFileAsync(
     this Uri uri,
     StorageFile targetFile)
 {
     using (var fileStream = await targetFile.OpenAsync(FileAccessMode.ReadWrite).AsTask().ConfigureAwait(false))
     {
         using (var request = new HttpHelperRequest(uri, HttpMethod.Get))
         {
             using (var response = await HttpHelper.Instance.SendRequestAsync(request).ConfigureAwait(false))
             {
                 if (response.Success)
                 {
                     await response.Content.WriteToStreamAsync(fileStream).AsTask().ConfigureAwait(false);
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Get the response stream returned by a HTTP get request.
        /// </summary>
        /// <param name="uri">Uri to request.</param>
        /// <returns>Response stream</returns>
        public static async Task <IRandomAccessStream> GetHttpStreamAsync(this Uri uri)
        {
            var outputStream = new InMemoryRandomAccessStream();

            using (var request = new HttpHelperRequest(uri, HttpMethod.Get))
            {
                using (var response = await HttpHelper.Instance.SendRequestAsync(request).ConfigureAwait(false))
                {
                    if (response.Success)
                    {
                        await response.Content.WriteToStreamAsync(outputStream).AsTask().ConfigureAwait(false);

                        outputStream.Seek(0);
                    }
                }
            }

            return(outputStream);
        }
        /// <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();
            }
        }