コード例 #1
0
ファイル: HttpRequest.cs プロジェクト: zhaowd2001/simpleflow
        /// <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);
        }
コード例 #2
0
ファイル: Environment.cs プロジェクト: zhaowd2001/simpleflow
 /// <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
        /*
        // Sample code of calling a AMicroblog API
        public void Post(string status)
        {
        try
        {
        var updStatusInfo = new UpdateStatusInfo();
        updStatusInfo.Status = status;

        var statusInfo = AMicroblog.PostStatus(updStatusInfo);

        // Further post-process against statusInfo...
        }
        catch (AMicroblogException aex)
        {
        // Handles the exception only if the exception is not handled by Unified Response Error Handling mechanism.
        if (!aex.IsHandled)
        {
            if (aex.IsRemoteError)
            {
                ShowErrorMessage("Server returned an error: {0}", aex.Message);

                // You can customize the error message based on aex.RemoteErrorCode.
            }
            else
                ShowErrorMessage("Unexpected error occured: {0}", aex.Message);
        }
        }
        catch (Exception ex)
        {
        ShowErrorMessage("Unexpected error occured: {0}", ex.Message);
        }
        }
        */
        private void Environment_ResponseError(object sender, ResponseErrorEventArgs e)
        {
            Console.WriteLine("An error response for {0}: {1}", e.ErrorData.RequestUri, e.ErrorData.Exception.Message);
        }
コード例 #4
0
ファイル: Environment.cs プロジェクト: zhaowd2001/simpleflow
 /// <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 { }
         }
     }
 }