Asp.net mvc3 C#,跟踪500错误响应消息?

时间:2012-06-18 21:31:23

标签: c# asp.net httpwebresponse

我需要从HttpWebResponse获取500错误响应消息。

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
      post_response = responseStream.ReadToEnd();
      responseStream.Close();
}

enter image description here

谢谢!

[编辑]

使用浏览器

打开此网址

https://www.sagepayments.net/web_services/vterm_extensions/transaction_processing.asmx/BANKCARD_PRIOR_AUTH_SALE

浏览器将打印响应消息“缺少参数:M_ID。”

现在,我想使用asp.net

获取响应消息
var post_string = "hello=hi";

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://www.sagepayments.net/web_services/vterm_extensions/transaction_processing.asmx/BANKCARD_PRIOR_AUTH_SALE");
    objRequest.Method = "POST";
    objRequest.ContentLength = post_string.Length;
    objRequest.ContentType = "application/x-www-form-urlencoded";

    // post data is sent as a stream
    StreamWriter myWriter = null;
    myWriter = new StreamWriter(objRequest.GetRequestStream());
    myWriter.Write(post_string);
    myWriter.Close();

    // returned values are returned as a stream, then read into a string

    try
    {
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
        post_response = responseStream.ReadToEnd();
        responseStream.Close();
    }

    Response.Write(post_response);
    }
    catch(WebException e)
    {   
    Response.Write("Error : "+e.Message); 
    Response.Write("<br /> Data : " + e.Data);
    Response.Write("<br /> HelpLink : " + e.HelpLink);
    Response.Write("<br /> InnerException : " + e.InnerException);
    Response.Write("<br /> Response : " + e.Response);
    Response.Write("<br /> Source : " + e.Source);
    Response.Write("<br /> Status : " + e.Status);
    Response.Write("<br /> TargetSite : " + e.TargetSite);
    }


Result : 
Error : The remote server returned an error: (500) Internal Server Error.
Data : System.Collections.ListDictionaryInternal
HelpLink :
InnerException :
Response : System.Net.HttpWebResponse
Source : System
Status : ProtocolError
TargetSite : System.Net.WebResponse GetResponse()

如何收到此消息?

缺少参数:M_ID。

3 个答案:

答案 0 :(得分:4)

我认为您需要检查StatusCode&amp;如果在Web请求期间抛出异常,则Web响应的StatusDescription属性。

例如:

try
{
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
        post_response = responseStream.ReadToEnd();
        responseStream.Close();
    }
}
catch(WebException wex)
{
    // This is the line that gets you the response object
    HttpWebResponse response = (HttpWebResponse)wex.Response;

    if(response != null)
    {
        // You can now read the response StatusCode and StatusDescription
        HttpStatusCode responseCode = response.StatusCode;
        String statusDescription = response.StatusDescription;

        // Add your status checking logic here
    }
}

答案 1 :(得分:0)

除非您启用了自定义错误,否则将在IIS级别处理HTTP错误。查看此帖子,了解如何更改您的网络配置以添加您自己的自定义错误页面:Returning 404 Error ASP.NET MVC 3

这个问题: How to send a Status Code 500 in ASP.Net and still write to the response?

...还具体说明了如何抛出500以及如何做客户错误。它还包括Context.Response.TrySkipIisCustomErrors上的一点,这是接受的答案。

答案 2 :(得分:0)

这在我的情况下有效。希望它能对您有所帮助。

try
{
    response = (HttpWebResponse)request.GetResponse();
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        responseMessage = streamReader.ReadToEnd();
        Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
        response.Close();
    }
}
catch(WebException ex)
{
    using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream()))
    {
        responseMessage = streamReader.ReadToEnd();
        File.WriteAllText(@"C:\Users\Paul\Documents\text.txt", responseMessage);
    }
}