如何处理远程服务器500错误

时间:2014-01-17 11:45:45

标签: c# json mailchimp remote-server

我使用第三方API向MailChimp发送数据,当我发送Json请求时,远程服务器返回500错误。

它意味着在Json中返回错误,但我无法得到错误消息。

我尝试过使用HttpWebRequest,然后在带有WebException catch的try catch中使用它,但这仍然无济于事。

关于如何捕捉错误是什么的任何想法?

错误消息:

The remote server returned an error: (500) Internal Server Error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500)     Internal Server Error.

Source Error:


Line 1517:                    streamWriter.Close();
Line 1518:
Line 1519:                    var httpResponse =    (HttpWebResponse)httpWebRequest.GetResponse();
Line 1520:                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
Line 1521:                    {


Source File: \App_Code\c#\MailChimpApi.cs    Line: 1519

Stack Trace:


[WebException: The remote server returned an error: (500) Internal Server Error.]
System.Net.HttpWebRequest.GetResponse() +8527068
MailChimpManager.MakeAPICall(String apiAction, Object args) in \App_Code\c#\MailChimpApi.cs:1519

[MailChimpAPIException: You must specify a apikey value]
MailChimpManager.MakeAPICall(String apiAction, Object args) in \App_Code\c#\MailChimpApi.cs:1549
MailChimpManager.GetAllMembersForList(String API_KEY, String listId, String status, Int32 start, Int32 limit, String sort_field, String sort_dir) in \App_Code\c#\MailChimpApi.cs:784
__cms_form_send_newsletter.GetSyncMailChimps() in \__cms\form_send_newsletter.aspx.vb:73
__cms_form_send_newsletter.Page_Load(Object sender, EventArgs e) in \__cms\form_send_newsletter.aspx.vb:14
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

请求代码:

try
        {
            ////  Call the API with the passed arguments:
            //var resultString = fullUrl.PostJsonToUrl(args);
            //results = resultString.Trim().FromJson<T>();

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(fullUrl);
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = args.ToString();
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    results = streamReader.ReadToEnd().Trim().FromJson<T>();
                }
            }
        }
        catch (WebException ex)
        {
            string errorBody = ex.GetResponseBody();

            //  Serialize the error information:
            ApiError apiError = errorBody.FromJson<ApiError>();

            //  Throw a new exception based on this information:
            throw new MailChimpAPIException(apiError.Error, ex, apiError);
        }
        catch (Exception ex)
        {
            string errorBody = ex.GetResponseBody();

            //  Serialize the error information:
            ApiError apiError = errorBody.FromJson<ApiError>();

            //  Throw a new exception based on this information:
            throw new MailChimpAPIException(apiError.Error, ex, apiError);
        }

0 个答案:

没有答案