示例#1
0
        private async Task <ResponseWrapper> DoSendAsync(HttpWebRequest request)
        {
            ResponseWrapper responseWrapper = new ResponseWrapper();

            try
            {
                AddToOngoing(request);
                var response = (HttpWebResponse)await request.GetResponseAsync().ConfigureAwait(AwaitContinueOnCapturedContext);

                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    responseWrapper.Data = streamReader.ReadToEnd();
                }

                responseWrapper.StatusCode = response.StatusCode;
                authenticationStrategy.OnResponse(response);
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                if (response == null)
                {
                    throw new ServerUnavailableException("Server is unavailable", ex);
                }
                else
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new InvalidCredentialException("Credentials are invalid");
                    }
                    if (response.StatusCode == HttpStatusCode.RequestTimeout)
                    {
                        throw new WebException(ex.Message, WebExceptionStatus.Timeout);
                    }

                    string body = string.Empty;
                    try
                    {
                        using (var streamReader = new StreamReader(response.GetResponseStream()))
                        {
                            body = streamReader.ReadToEnd();
                        }
                    }
                    catch
                    {
                        // If anything goes wrong in the reading of the server response we still want to throw
                        // an exception with the original exception as inner.
                        throw ex;
                    }

                    try
                    {
                        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                        if (body.Contains("total_count"))
                        {
                            RestExceptionInfos exceptionInfos = jsSerializer.Deserialize <RestExceptionInfos>(body);
                            throw new MqmRestException(exceptionInfos.errors[0], response.StatusCode, ex);
                        }
                        else
                        {
                            RestExceptionInfo exceptionInfo = jsSerializer.Deserialize <RestExceptionInfo>(body);
                            throw new MqmRestException(exceptionInfo, response.StatusCode, ex);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is MqmRestException)
                        {
                            throw e;
                        }
                        else
                        {
                            throw new GeneralHttpException(body, response.StatusCode);
                        }
                    }
                }
            }
            finally
            {
                RemoveFromOngoing(request);
            }

            return(responseWrapper);
        }