WebClient:忽略HTTP 500 [英] WebClient: Ignore HTTP 500
问题描述
我正在编写一个程序,该程序从服务器检索一些数据,对其进行一些操作,然后将输出保存到一个csv文件中.我的问题是服务器(我不负责)始终返回HTTP 500内部服务器错误.我已经与负责此事的团队进行了交谈,虽然他们知道该错误,但他们表示该错误影响力不足,无法解决.
I am writing a program which retrieves some data from a server, does some operations on it, and saves the output to a csv file. The problem I have is that the server (which I am not responsible for) ALWAYS returns an HTTP 500 internal server error. I have spoken to the team who look after it, and while they're aware of the bug they've said it's not impacting enough for them to resolve.
我是否有办法忽略代码中的此响应并仍然获取数据?
Is there a way for me to ignore this response in my code and still get at the data?
推荐答案
如果您使用的是HttpWebRequest/Response,这应该可以帮助您入门:
If you're using HttpWebRequest/Response, this should get you started:
response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<url>");
response = (HttpWebResponse)request.GetResponse();
//no error
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
response = (HttpWebResponse)e.Response;
if((int)response.StatusCode == 500)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
var result = sr.ReadToEnd();
}
}
}
}
这篇关于WebClient:忽略HTTP 500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!