Wednesday, January 17, 2024

Get Web Request Error 500,400 Exception details in c#

WebException is a good approch to get the Real cause of 500 error,  


catch (WebException ex)

{
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        Console.WriteLine(reader.ReadToEnd());
    }
}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached
}

No comments:

Post a Comment