public async Task <IHttpWebResponse> GetResponseAsync()
        {
            try
            {
                using (var clientHandler = (HttpClientHandler)HttpMessageHandlerFactory.GetMessageHandler(this.UseDefaultCredentials))
                    return(await GetResponseAsync(clientHandler));
            }
            catch (HttpRequestException ex)
            {
                var            innerException = (WebException)ex.InnerException;
                HttpStatusCode statusCode     = (innerException?.Response as HttpWebResponse)?.StatusCode ?? 0;

                if (statusCode != HttpStatusCode.ProxyAuthenticationRequired)
                {
                    throw;
                }

                //Got proxy authenication failure (407) - configuring proxy

                var proxyUrl = innerException.Response.ResponseUri;
                using (
                    var clientHandler =
                        (HttpClientHandler)HttpMessageHandlerFactory.GetMessageHandler(this.UseDefaultCredentials))
                {
                    HttpMessageHandlerFactory.InvokeUpdateWebProxy(clientHandler, proxyUrl);

                    return(await GetResponseAsync(clientHandler));
                }
            }
        }
Beispiel #2
0
        public async Task <IHttpWebResponse> GetResponseAsync()
        {
            using (HttpClient client =
                       new HttpClient(HttpMessageHandlerFactory.GetMessageHandler(this.UseDefaultCredentials)))
            {
                client.MaxResponseContentBufferSize = _maxResponseSizeInBytes;
                client.DefaultRequestHeaders.Accept.Clear();
                HttpRequestMessage requestMessage = new HttpRequestMessage();
                requestMessage.RequestUri = new Uri(uri);
                requestMessage.Headers.Accept.Clear();

                requestMessage.Headers.Accept.Add(
                    new MediaTypeWithQualityHeaderValue(this.Accept ?? "application/json"));
                foreach (KeyValuePair <string, string> kvp in this.Headers)
                {
                    requestMessage.Headers.Add(kvp.Key, kvp.Value);
                }

                bool addCorrelationId = (this.CallState != null && this.CallState.CorrelationId != Guid.Empty);
                if (addCorrelationId)
                {
                    requestMessage.Headers.Add(OAuthHeader.CorrelationId, this.CallState.CorrelationId.ToString());
                    requestMessage.Headers.Add(OAuthHeader.RequestCorrelationIdInResponse, "true");
                }

                client.Timeout = TimeSpan.FromMilliseconds(this._timeoutInMilliSeconds);

                HttpResponseMessage responseMessage;

                try
                {
                    if (this.BodyParameters != null)
                    {
                        HttpContent content;
                        if (this.BodyParameters is StringRequestParameters)
                        {
                            content = new StringContent(this.BodyParameters.ToString(), Encoding.UTF8,
                                                        this.ContentType);
                        }
                        else
                        {
                            content = new FormUrlEncodedContent(((DictionaryRequestParameters)this.BodyParameters)
                                                                .ToList());
                        }

                        requestMessage.Method  = HttpMethod.Post;
                        requestMessage.Content = content;
                    }
                    else
                    {
                        requestMessage.Method = HttpMethod.Get;
                    }

                    responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false);
                }
                catch (TaskCanceledException ex)
                {
                    throw new HttpRequestWrapperException(null, ex);
                }

                IHttpWebResponse webResponse = await CreateResponseAsync(responseMessage).ConfigureAwait(false);

                if (!responseMessage.IsSuccessStatusCode)
                {
                    try
                    {
                        throw new HttpRequestException(
                                  string.Format(CultureInfo.CurrentCulture,
                                                " Response status code does not indicate success: {0} ({1}).",
                                                (int)webResponse.StatusCode, webResponse.StatusCode),
                                  new AdalException(webResponse.ResponseString));
                    }
                    catch (HttpRequestException ex)
                    {
                        throw new HttpRequestWrapperException(webResponse, ex);
                    }
                }

                if (addCorrelationId)
                {
                    VerifyCorrelationIdHeaderInReponse(webResponse.Headers);
                }

                return(webResponse);
            }
        }