问题
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?
回答1:
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();
}
}
}
}
来源:https://stackoverflow.com/questions/13338894/webclient-ignore-http-500