/// <summary>
 ///   Initializes a new instance of <see cref="ResponseErrorEventArgs" /> with the specified parameters.
 /// </summary>
 /// <param name="requestUri"> The request uri. </param>
 /// <param name="exception"> The exception of <see cref="AMicroblogException" /> type. </param>
 /// <param name="httpMethod"> The request method. </param>
 /// <param name="contentType"> The request content type. </param>
 public ResponseErrorEventArgs(string requestUri, SinaWeiboException exception, string httpMethod,
                               string contentType)
 {
     ErrorData = new SinaResponseErrorData();
     ErrorData.ErrorCode = exception.ErrorCode;
     ErrorData.RequestUri = requestUri;
     ErrorData.Exception = exception;
     ErrorData.HttpMethod = httpMethod;
     ErrorData.ContentType = contentType;
 }
Exemple #2
0
        /// <summary>
        ///   Performs the HTTP request.
        /// </summary>
        /// <returns> The server response string(UTF8 decoded). </returns>
        public virtual string Request()
        {
            var req = WebRequest.Create(ConstructUri()) as HttpWebRequest;

            AppendHeaders(req.Headers);

            if (!string.IsNullOrEmpty(Method))
            {
                req.Method = Method;
            }

            if (!string.IsNullOrEmpty(ContentType))
            {
                req.ContentType = ContentType;
            }

            PrepareRequest(req);

            // Calls GetRequestStream with a "GET" method, an exception is thrown "Cannot send a content-body with this verb-type."
            if (req.Method == HttpMethod.Post)
            {
                using (Stream reqStream = req.GetRequestStream())
                {
                    WriteBody(reqStream);
                }

                // ContentLength is automatically calculated.
            }

            WebResponse resp = null;
            try
            {
                resp = req.GetResponse();
            }
            catch (WebException wex)
            {
                ErrorResponse errorResp;
                var webResp = wex.Response as HttpWebResponse;
                if (null == webResp)
                    throw new SinaWeiboException(LocalErrorCode.NetworkUnavailable);

                try
                {
                    string responseContent = RetrieveResponse(webResp);

                    if (!string.IsNullOrEmpty(responseContent) &&
                        responseContent.StartsWith(SinaConstants.JsonHeader, StringComparison.InvariantCultureIgnoreCase))
                    {
                        errorResp = JsonSerializationHelper.JsonToObject<ErrorResponse>(responseContent);

                        //TODO: 21327 token expired
                    }
                    else
                    {
                        // Handle other cases
                        errorResp = new ErrorResponse
                                        {ErrorCode = (int) webResp.StatusCode, ErrorMessage = webResp.StatusDescription};
                    }
                }
                catch (Exception exx)
                {
                    throw new SinaWeiboException(LocalErrorCode.ProcessResponseErrorHandlingFailed, exx);
                }

                var aex = new SinaWeiboException(errorResp.ErrorCode, wex)
                              {IsRemoteError = true, ErrorCode = errorResp.ErrorCode};

                var e = new ResponseErrorEventArgs(req.RequestUri.AbsoluteUri, aex, req.Method, req.ContentType);

                throw aex;
            }

            return RetrieveResponse(resp);
        }