/// <summary> /// Asynchronously sends a request to the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="param">The request parameters.</param> /// <param name="accept">The content types to accept.</param> /// <returns></returns> public async Task <HttpResponseMessage> SendAsync(Uri url, RequestParams param, string accept) { var newParams = param ?? new RequestParams(); while (true) { // Store params m_url = url; m_accept = accept; HttpResponseMessage response = null; try { var request = GetHttpRequest(newParams); response = await GetHttpResponseAsync(GetHttpClientHandler(), request). ConfigureAwait(false); EnsureSuccessStatusCode(response); } catch (HttpWebClientServiceException) { // Seems pointless but prevents the exception from getting wrapped again throw; } catch (HttpRequestException ex) { // Strip a layer of exceptions if a web exception occurred if (ex.InnerException is WebException) { throw HttpWebClientServiceException.HttpWebClientException(url, ex.InnerException); } // Throw default exception if no response if (response == null) { throw HttpWebClientServiceException.Exception(url, ex); } // Throw for 404, 500, etc. if (response.StatusCode != HttpStatusCode.Redirect && response. StatusCode != HttpStatusCode.MovedPermanently) { throw HttpWebClientServiceException.HttpWebClientException(url, ex, response.StatusCode); } } catch (TaskCanceledException ex) { // We throw a request timeout if the task gets cancelled due to the // timeout setting throw HttpWebClientServiceException.HttpWebClientException(url, new HttpRequestException(ex.Message), HttpStatusCode.RequestTimeout); } catch (Exception ex) { throw HttpWebClientServiceException.Exception(url, ex); } if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently) { return(response); } // When the address has been redirected, connects to the redirection Uri target = response.Headers.Location; response.Dispose(); if (m_redirectsRemaining-- <= 0) { throw HttpWebClientServiceException.RedirectsExceededException(m_url); } m_referrer = m_url; m_url = new Uri(m_url, target); url = m_url; } }
/// <summary> /// Asynchronously sends a request to the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="method">The method.</param> /// <param name="postData">The post data.</param> /// <param name="dataCompression">The data compression.</param> /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param> /// <param name="accept">The accept.</param> /// <returns></returns> public async Task <HttpResponseMessage> SendAsync(Uri url, HttpMethod method, HttpPostData postData, DataCompression dataCompression, bool acceptEncoded, string accept) { while (true) { // Store params m_url = url; m_accept = accept; m_postData = postData; m_method = postData == null || method == null ? HttpMethod.Get : method; m_dataCompression = postData == null ? DataCompression.None : dataCompression; m_acceptEncoded = acceptEncoded; HttpResponseMessage response = null; try { HttpClientHandler httpClientHandler = GetHttpClientHandler(); HttpRequestMessage request = GetHttpRequest(); response = await GetHttpResponseAsync(httpClientHandler, request).ConfigureAwait(false); EnsureSuccessStatusCode(response); } catch (HttpWebClientServiceException) { throw; } catch (HttpRequestException ex) { if (ex.InnerException is WebException) { throw HttpWebClientServiceException.HttpWebClientException(url, ex.InnerException); } if (response == null) { throw HttpWebClientServiceException.Exception(url, ex); } if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently) { throw HttpWebClientServiceException.HttpWebClientException(url, ex, response.StatusCode); } } catch (WebException ex) { // We should not get a WebException here but keep this as extra precaution throw HttpWebClientServiceException.HttpWebClientException(url, ex); } catch (TaskCanceledException ex) { // We throw a request timeout if the task gets cancelled due to the timeout setting throw HttpWebClientServiceException.HttpWebClientException(url, new HttpRequestException(ex.Message), HttpStatusCode.RequestTimeout); } catch (Exception ex) { throw HttpWebClientServiceException.Exception(url, ex); } if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently) { return(response); } // When the address has been redirected, connects to the redirection Uri target = response.Headers.Location; response.Dispose(); if (m_redirectsRemaining-- <= 0) { throw HttpWebClientServiceException.RedirectsExceededException(m_url); } m_referrer = m_url; m_url = new Uri(m_url, target); url = m_url; } }