Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest accept 500 Internal Server Error

This is my code:

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

WebResponse wr = req.GetResponse();

When the server returns 500 Internal Server Error, exception is thrown in req.GetResponse(). I would like the GetResponse() to accept this Response Code, it is normal for the passed url to throw this Response Code. I would like to parse the Html despite Response Code 500 Internal Server Error. Is it possible to say to GetResponse() method not to verify the Response Code?

like image 740
Darxis Avatar asked Aug 23 '13 13:08

Darxis


People also ask

How do I fix the remote server returned an error 500 internal server error?

This can be a php timeout. In such cases, the webserver will return a 500 Internal Server Error. We can fix this error by increasing timeout values or setting other appropriate timeout options so that the remote server will not return a timeout error but wait for the request to be processed.

Why do I keep getting error 500?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable.

What is HttpWebRequest C#?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP. Do not use the HttpWebRequest constructor.


1 Answers

try
{
    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

    WebResponse wr = req.GetResponse();
}
catch (WebException wex)
{
    var pageContent = new StreamReader(wex.Response.GetResponseStream())
                          .ReadToEnd();
}
like image 163
I4V Avatar answered Sep 19 '22 04:09

I4V