1

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

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

在此处输入图像描述

谢谢!

[编辑]

使用浏览器打开此网址,

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。

4

3 回答 3

4

如果在 Web 请求期间引发异常,我认为您需要检查 Web 响应的 StatusCode 和 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
    }
}
于 2012-06-18T22:10:27.000 回答
0

除非您打开了自定义错误,否则 HTTP 错误将在 IIS 级别处理。查看这篇文章,了解如何更改您的 Web 配置以添加您自己的自定义错误页面:返回 404 错误 ASP.NET MVC 3

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

...还特别介绍了如何抛出 500 以及如何处理客户错误。它还包括一些关于Context.Response.TrySkipIisCustomErrors哪个是公认的答案。

于 2012-06-18T21:51:43.790 回答
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);
    }
}
于 2019-08-28T15:08:36.777 回答