How to process WebResponse when .NET throws WebException ((400) Bad Request)?
Asked Answered
R

2

41

I'm using Facebook Graph Api and trying to get user data. I'm sending user access token and in case this token is expired or invalid Facebook returns status code 400 and this response:

{
    "error": {
        "message": "Error validating access token: The session is invalid because the user logged out.",
        "type": "OAuthException"
    }
}

The problem is that when I use this C# code:

try {
   webResponse = webRequest.GetResponse(); // in case of status code 400 .NET throws WebException here
} catch (WebException ex) {
}

If status code is 400 .NET throws WebException and my webResponse is null after exception is caught so I have no chance to process it. I want to do it to make sure that the problem is in expired token and not somewhere else.

Is there a way to do it?

Thanks.

Reflation answered 30/9, 2011 at 10:38 Comment(0)
H
101

Using a try/catch block like this and processing the error message appropriately should work fine:

    var request = (HttpWebRequest)WebRequest.Create(address);
    try {
        using (var response = request.GetResponse() as HttpWebResponse) {
            if (request.HaveResponse && response != null) {
                using (var reader = new StreamReader(response.GetResponseStream())) {
                    string result = reader.ReadToEnd();
                }
            }
        }
    }
    catch (WebException wex) {
        if (wex.Response != null) {
            using (var errorResponse = (HttpWebResponse)wex.Response) {
                using (var reader = new StreamReader(errorResponse.GetResponseStream())) {
                    string error = reader.ReadToEnd();
                    //TODO: use JSON.net to parse this string and look at the error message
                }
            }
        }
    }
}

However, using the Facebook C# SDK makes this all really easy so that you don't have to process this yourself.

Hairline answered 1/10, 2011 at 5:30 Comment(4)
Thanks for detailed answer, I've done it alredy in a similar way. I know that I could use Facebook SDK, but using Facebook api is relatively easy in comparison with Google or Twitter ones so I decided to do everything manually here to understand the flow and to have more control.Reflation
Anyone knows why WebException --> errorResponse.GetResponseStream() is null in Silverlight? I can see the body response in Fiddler though.Ask
It's null for me too. Doing a bit of digging. This works as expected when compiled in normal .net.Tredecillion
The issue of losing the WebException->errorResponse seems to come from the using block. Following the advice given in this How to Answer (https://mcmap.net/q/392399/-how-to-properly-dispose-of-a-webresponse-instance), if you just use a using block around the stream, you don't lose the errorResponse.Foskett
V
14

The WebException still has the "real" response in the Response property (assuming there was a response at all) so you can get the data from that within the catch block.

Veneering answered 30/9, 2011 at 10:41 Comment(2)
Thanks Jon, is it a good idea to try to access Response property there? what will happen if I get an exception inside catch block? should I put my try and catch inside another try and catch?Reflation
@Burjua: Accessing the property won't give you an exception - that's what it's there for, after all. I believe that the response will already contain all the response data, so reading the response stream itself should be safe if you manage to acquire it.Veneering

© 2022 - 2024 — McMap. All rights reserved.