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.

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
Burjua
  • 12,506
  • 27
  • 80
  • 111

2 Answers2

100

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.

bkaid
  • 51,465
  • 22
  • 112
  • 128
  • 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. – Burjua Oct 04 '11 at 10:59
  • Anyone knows why WebException --> errorResponse.GetResponseStream() is null in Silverlight? I can see the body response in Fiddler though. – Ismael Nov 21 '12 at 10:25
  • It's null for me too. Doing a bit of digging. This works as expected when compiled in normal .net. – Ross Jones Mar 27 '13 at 11:11
  • The issue of losing the WebException->errorResponse seems to come from the `using` block. Following the advice given in this [answer] (http://stackoverflow.com/a/1887407/1547089), if you just use a `using` block around the stream, you don't lose the errorResponse. – ksaylor11 Jul 22 '15 at 17:06
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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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`? – Burjua Sep 30 '11 at 10:47
  • @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. – Jon Skeet Sep 30 '11 at 10:48