500使用HttpWebRequest时出现内部服务器错误,如何获得真正的错误? [英] 500 Internal Server Error when using HttpWebRequest, how can I get to the real error?

查看:315
本文介绍了500使用HttpWebRequest时出现内部服务器错误,如何获得真正的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试改善所提供的信息,以应对应用程序内处理的错误.

I'm trying to improve the information provided in response to an error handled within an app.

这是代码:

Try
        httpRequestObj = HttpWebRequest.Create(strRequest)
        httpRequestObj.Method = "GET"
        httpRequestObj.UseDefaultCredentials = True
*       httpResponse = httpRequestObj.GetResponse
        Using reader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
            strXML = reader.ReadToEnd()
        End Using
    Catch ex As WebException
        'do something with ex
    End Try

在*行上抛出了webexception

The webexception is thrown on the * line

当前,我在异常中看到的全部是远程服务器返回错误:(500)内部服务器错误".我已经在调试中查看了异常,但是所需的信息却不存在-我想需要读取响应才能看到该信息,但它永远不会走那么远.

Currently all I see in the Exception is "The remote server returned an error: (500) Internal Server Error". I've looked at the exception in debug but the info I need isn't there- I guess the response would need to be read in to see that info but it never gets that far.

如果我接受请求并将其直接粘贴到浏览器中,则可以看到我正在调用的API返回的XML格式的错误详细信息,

If I take the request and paste it into my browser directly I can see the error details in XML format that is returned from the API I'm calling, info like:

<Error>
  <description>info I want to get to here</description> 
  <detail /> 
  <code>info I want to get to here</code> 
  <source /> 
  <category>info I want to get to here</category> 
  <file>info I want to get to here</file> 
  <line>info I want to get to here</line> 
  <pad /> 
</Error>

有什么办法可以更改此代码,以便克服500错误并查看实际响应,我希望能够解析此xml来找出失败的真正问题.

Is there any way I can change this code so that I can get past the 500 error and see the actual response, I'd like to be able to parse this xml to find out the real problem for the failure.

注意:异常确实有一个ex.Response(System.Net.HttpWebResponse),但我在那里看不到我需要的信息,只有一堆Header信息.

Note: the Exception does have an ex.Response (System.Net.HttpWebResponse), but I can't see the info I need in there, only a load of Header info.

推荐答案

您可以从异常中获取错误响应....

You can get the error response from the exception....

try
{
....
} catch(Exception e) {
   if (e is WebException && ((WebException)e).Status==WebExceptionStatus.ProtocolError)
   {
      WebResponse errResp = ((WebException)e).Response;
      using(Stream respStream = errResp.GetResponseStream())
      {
         // read the error response
      }
   }
}

这篇关于500使用HttpWebRequest时出现内部服务器错误,如何获得真正的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆