WebClient: Ignore HTTP 500

天涯浪子 提交于 2020-01-02 03:42:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!