Search code examples
c#webclienthttpresponsehttp-status-code-403

Need to read response of 403 Forbidden with WebClient C#


I am using C# WebClient to get a server response and i get this exception message :

The remote server returned an error: (403) Forbidden.

this is right page response code is 403 but there is additional response message i need to read. this response message is the same i get when i access the server from a web browser.

The first image is showing HTTP headers in chrome. and the second one shows the response.

what i am asking for is how to get this text/plain response message ?

http headers in chrome


http response


Solution

  • Catch the WebException and read its Response property:

    try
    {
        request.GetResponse();
    }
    catch (WebException e)
    {
        if (e.Response != null)
            return getResponseBody(e.Response);
        else
            throw;
    }