Пример #1
0
        private tsResponse GetResponse(int maxAttempts = Constants.DefaultMaxRequestAttempts)
        {
            int attempt = 1;

            while (attempt <= maxAttempts)
            {
                HttpWebRequest request = this.ToHttpWebRequest();
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (!IsSuccessfulStatusCode(response.StatusCode))
                    {
                        throw new HttpRequestException(String.Format("Received non-successful status code {0} ({1})", response.StatusCode, response.StatusDescription));
                    }

                    if (response.ContentLength == 0)
                    {
                        return(new tsResponse());
                    }

                    return(response.Deserialize());
                }
                catch (Exception ex)
                {
                    if (attempt == maxAttempts)
                    {
                        throw new HttpRequestException(String.Format("Failed to retrieve successful response for {0} request to '{1}' after {2} attempts: {3}",
                                                                     Method, Uri, maxAttempts, ex.Message), ex);
                    }
                    attempt++;
                }
            }

            throw new HttpRequestException(String.Format("Failed to retrieve successful response for {0} request to '{1}' after {2} attempts.", Method, Uri, maxAttempts));
        }
Пример #2
0
        async protected Task <ApiResponse> Complete(HttpWebRequest request)
        {
            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)await request.GetResponseAsync();

                return(new ApiResponse {
                    HttpResponse = response
                });
            }
            catch (WebException wex)
            {
                response = wex.Response as HttpWebResponse;
                if (response == null)
                {
                    throw;
                }
            }
            finally
            {
                if (response != null)
                {
                    OnRequestCompleted(response);
                }
            }

            var errorResponse = response.Deserialize <GitHubErrorResponse>();

            try
            {
                throw GitHubErrorExceptionFactory.From(response, errorResponse);
            }
            catch (UnauthorizedException)
            {
                Context.ClearAuthorization();
                throw;
            }
        }