예제 #1
0
파일: Environment.cs 프로젝트: dkme/moooyo
 /// <summary>
 /// Triggers the <see cref="ResponseError"/>.
 /// </summary>
 /// <remarks>To insure that each event handler has a chance to process the event, any exception is suspended.</remarks>
 internal static void NotifyResponseError(ResponseErrorEventArgs e)
 {
     if (null != ResponseError)
     {
         var invocationList = ResponseError.GetInvocationList();
         foreach (var invocation in invocationList)
         {
             try
             {
                 var inv = invocation as EventHandler<ResponseErrorEventArgs>;
                 inv(null, e);
             }
             catch { }
         }
     }
 }
예제 #2
0
파일: Environment.cs 프로젝트: dkme/moooyo
 /// <summary>
 /// Handles the response error event.
 /// </summary>
 private static void HandleResponseError(object sender, ResponseErrorEventArgs e)
 {
     foreach (var handler in ResponseErrorHandlers)
     {
         try
         {
             var errorCode = e.ErrorData.ErrorCode.ToString();
             if (handler.ErrorCode == "*" || errorCode == handler.ErrorCode || Regex.IsMatch(errorCode, handler.ErrorCode, RegexOptions.IgnoreCase))
             {
                 var realHandler = Activator.CreateInstance(handler.Type) as IResponseErrorHandler;
                 realHandler.Handle(e.ErrorData);
                 e.IsHandled = true;
             }
         }
         catch
         {
             // Nothing to do.
         }
     }
 }
예제 #3
0
파일: HttpRequest.cs 프로젝트: dkme/moooyo
        /// <summary>
        /// Performs the HTTP request.
        /// </summary>
        /// <returns>The server response string(UTF8 decoded).</returns>
        public virtual string Request()
        {
            HttpWebRequest req = HttpWebRequest.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 (var 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 AMicroblogException(LocalErrorCode.NetworkUnavailable);

                try
                {
                    var responseContent = RetrieveResponse(webResp);

                    if (!string.IsNullOrEmpty(responseContent) && responseContent.StartsWith(Constants.XmlHeader, StringComparison.InvariantCultureIgnoreCase))
                    {
                        errorResp = XmlSerializationHelper.XmlToObject<ErrorResponse>(responseContent);
                        // Retrieves the error code and wraps the exception.
                        var errorCode = AMicroblogException.RetrieveErrorCode(errorResp.ErrorMessage);
                        if (!string.IsNullOrEmpty(errorCode))
                            errorResp.ErrorCode = int.Parse(errorCode);
                    }
                    else
                    {
                        // Handle other cases
                        errorResp = new ErrorResponse() { ErrorCode = (int)webResp.StatusCode, ErrorMessage = webResp.StatusDescription };
                    }
                }
                catch (Exception exx)
                {
                    throw new AMicroblogException(LocalErrorCode.ProcessResponseErrorHandlingFailed, exx);
                }

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

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

                aex.IsHandled = e.IsHandled;

                throw aex;
            }

            return RetrieveResponse(resp);
        }