HttpWebRequest.GetResponse() 失败时如何获取错误信息
我正在启动 HttpWebRequest,然后检索它的响应。有时,我会收到 500(或至少 5##)错误,但没有描述。我可以控制两个端点,并希望接收端获得更多信息。例如,我想将异常消息从服务器传递到客户端。使用 HttpWebRequest 和 HttpWebResponse 可以吗?
代码:
try
{
HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Credentials = new NetworkCredential(Username, Password);
webRequest.ContentType = "application/x-www-form-urlencoded";
using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
{
if(response.StatusCode == HttpStatusCode.OK)
{
// Do stuff with response.GetResponseStream();
}
}
}
catch(Exception ex)
{
ShowError(ex);
// if the server returns a 500 error than the webRequest.GetResponse() method
// throws an exception and all I get is "The remote server returned an error: (500)."
}
任何对此的帮助将不胜感激。
I am initiating an HttpWebRequest and then retrieving it's response. Occasionally, I get a 500 (or at least 5##) error, but no description. I have control over both endpoints and would like the receiving end to get a little bit more information. For example, I would like to pass the exception message from server to client. Is this possible using HttpWebRequest and HttpWebResponse?
Code:
try
{
HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Credentials = new NetworkCredential(Username, Password);
webRequest.ContentType = "application/x-www-form-urlencoded";
using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
{
if(response.StatusCode == HttpStatusCode.OK)
{
// Do stuff with response.GetResponseStream();
}
}
}
catch(Exception ex)
{
ShowError(ex);
// if the server returns a 500 error than the webRequest.GetResponse() method
// throws an exception and all I get is "The remote server returned an error: (500)."
}
Any help with this would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以让 Web 服务器简单地捕获异常文本并将其写入响应正文中,然后将状态代码设置为 500。现在,客户端在遇到 500 错误时会抛出异常,但您可以读取响应流并获取异常的消息。
因此,您可以捕获 WebException,如果非 200,则会抛出该异常从服务器返回状态码并读取其正文:
You could have your web server simply catch and write the exception text into the body of the response, then set status code to 500. Now the client would throw an exception when it encounters a 500 error but you could read the response stream and fetch the message of the exception.
So you could catch a WebException which is what will be thrown if a non 200 status code is returned from the server and read its body:
我在尝试检查 FTP 站点上是否存在文件时遇到了这个问题。如果该文件不存在,则在尝试检查其时间戳时将会出现错误。但我想通过检查错误的类型来确保错误不是其他错误。
WebException
上的Response
属性将是FtpWebResponse
类型,您可以检查其StatusCode
属性来查看您遇到的 FTP 错误。这是我最终得到的代码:
I came across this question when trying to check if a file existed on an FTP site or not. If the file doesn't exist there will be an error when trying to check its timestamp. But I want to make sure the error is not something else, by checking its type.
The
Response
property onWebException
will be of typeFtpWebResponse
on which you can check itsStatusCode
property to see which FTP error you have.Here's the code I ended up with:
我遇到了类似的情况:
我尝试使用 BasicHTTPBinding 读取原始响应,以防使用 SOAP 服务时发生 HTTP 错误。
但是,当使用
GetResponseStream()
读取响应时,出现错误:所以,这段代码对我有用:
I faced a similar situation:
I was trying to read raw response in case of an HTTP error consuming a SOAP service, using BasicHTTPBinding.
However, when reading the response using
GetResponseStream()
, got the error:So, this code worked for me:
您还可以使用此库,它将 HttpWebRequest 和 Response 包装成简单的方法,这些方法根据结果。它使用了这些答案中描述的一些技术,并且有大量受此答案和类似线程的答案启发的代码。它会自动捕获任何异常,尽可能抽象出发出这些 Web 请求所需的样板代码,并自动反序列化响应对象。
使用此包装器的代码示例就像
完全公开 一样简单
该库是一个免费的开源包装库,我是该库的作者。我没有从中赚钱,但多年来发现它非常有用,并且确信任何仍在使用 HttpWebRequest / HttpWebResponse 类的人也会如此。
它不是灵丹妙药,但支持异步和非异步的 get、post、delete,以及 JSON 或 XML 请求和响应。截至 2020 年 6 月 21 日,它正在积极维护中
You can also use this library which wraps HttpWebRequest and Response into simple methods that return objects based on the results. It uses some of the techniques described in these answers and has plenty of code inspired by answers from this and similar threads. It automatically catches any exceptions, seeks to abstract as much boiler plate code needed to make these web requests as possible, and automatically deserializes the response object.
An example of what your code would look like using this wrapper is as simple as
Full disclosure
This library is a free open source wrapper library, and I am the author of said library. I make no money off of this but have found it immensely useful over the years and am sure anyone who is still using the HttpWebRequest / HttpWebResponse classes will too.
It is not a silver bullet but supports get, post, delete with both async and non-async for get and post as well as JSON or XML requests and responses. It is being actively maintained as of 6/21/2020
有时
ex.Response
也会抛出 NullReferenceException 所以下面是处理的最佳方法Sometimes
ex.Response
also throws NullReferenceException so below is the best way to handle