Exemplo n.º 1
0
        public ResponseWrapper Send(string restRelativeUri, RequestType requestType, string data)
        {
            if (!IsConnected())
            {
                throw new NotConnectedException();
            }

            HttpWebRequest  request         = CreateRequest(restRelativeUri, requestType);
            ResponseWrapper responseWrapper = new ResponseWrapper();

            try
            {
                if ((requestType == RequestType.Post || requestType == RequestType.Update) && !String.IsNullOrEmpty(data))
                {
                    byte[] byteData = Encoding.UTF8.GetBytes(data);
                    request.ContentLength = byteData.Length;
                    using (Stream postStream = request.GetRequestStream())
                    {
                        postStream.Write(byteData, 0, byteData.Length);
                    }
                }


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

                responseWrapper.StatusCode = response.StatusCode;
                UpdateLwssoTokenFromResponse(response);
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                if (response == null)
                {
                    throw new ServerUnavailableException();
                }
                else
                {
                    String body = null;
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        body = streamReader.ReadToEnd();
                    }
                    JavaScriptSerializer jsSerializer  = new JavaScriptSerializer();
                    RestExceptionInfo    exceptionInfo = jsSerializer.Deserialize <RestExceptionInfo>(body);
                    throw new MqmRestException(exceptionInfo, response.StatusCode);
                }
            }

            return(responseWrapper);
        }
Exemplo n.º 2
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);
        }