Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient: Ignore HTTP 500

Tags:

c#

.net

webclient

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?

like image 415
Matt G Avatar asked Nov 12 '12 05:11

Matt G


1 Answers

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();
            }
        }
    }
}
like image 150
Chad Avatar answered Nov 11 '22 08:11

Chad