예제 #1
0
        private void PopulateFromMWSException(MwsException e)
        {
            if (e.StatusCode != null)
            {
                _statusCode = (int)e.StatusCode;
            }

            if (e.Message != null)
            {
                _message = e.Message;
            }

            if (e._errorCode != null)
            {
                _errorCode = e.ErrorCode;
            }

            if (e._errorType != null)
            {
                _errorType = e.ErrorType;
            }

            if (e._detail != null)
            {
                _detail = e.Detail;
            }

            if (e._xml != null)
            {
                _xml = e.XML;
            }

            if (e.ResponseHeaderMetadata != null)
            {
                _rhmd = e.ResponseHeaderMetadata;
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a request and invokes it.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="MwsException">Exceptions from invoking the request.</exception>
        public IMwsReader Invoke()
        {
            string responseBody;
            string message;
            var    statusCode = default(HttpStatusCode);

            /* Add required request parameters */
            AddRequiredParameters();
            var queryString = GetParametersAsString(_parameters);
            var retries     = 0;

            do
            {
                /* Submit the request and read response body */
                try
                {
                    request = _connection.GetHttpClient(_serviceEndPoint.URI);
                    var requestData = new UTF8Encoding().GetBytes(queryString);
                    request.ContentLength = requestData.Length;
                    using (var requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(requestData, 0, requestData.Length);
                    }

                    using (var httpResponse = request.GetResponse() as HttpWebResponse)
                    {
                        statusCode = httpResponse.StatusCode;
                        message    = httpResponse.StatusDescription;
                        _ResponseHeaderMetadata = GetResponseHeaderMetadata(httpResponse);
                        var reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
                        responseBody = reader.ReadToEnd();
                    }

                    if (statusCode == HttpStatusCode.OK)
                    {
                        return(new MwsXmlReader(responseBody));
                    }

                    var e = new MwsException((int)statusCode, message, null, null, responseBody, _ResponseHeaderMetadata);

                    if (statusCode == HttpStatusCode.InternalServerError)
                    {
                        if (PauseIfRetryNeeded(retries++))
                        {
                            continue;
                        }
                    }

                    throw e;
                }

                /* Web exception is thrown on unsuccessful responses */
                catch (WebException we)
                {
                    using (var httpErrorResponse = (HttpWebResponse)we.Response as HttpWebResponse)
                    {
                        if (httpErrorResponse == null)
                        {
                            throw new MwsException(we);
                        }

                        statusCode = httpErrorResponse.StatusCode;
                        using (var reader = new StreamReader(httpErrorResponse.GetResponseStream(), Encoding.UTF8))
                        {
                            responseBody = reader.ReadToEnd();
                        }
                    }

                    // retry logic
                    if (PauseIfRetryNeeded(retries++))
                    {
                        continue;
                    }

                    throw new MwsException((int)statusCode, null, null, null, responseBody, null);
                }

                /* Catch other exceptions, attempt to convert to formatted exception,
                 * else rethrow wrapped exception */
                catch (Exception e)
                {
                    throw new MwsException(e);
                }
            }while (true);
        }