I have a WebClient and I’m subscribing to the OnDownloadStringCompleted event handler. When the server responds with a header of 400 Bad Request, the OnDownloadStringCompleted Never gets triggered. I still need what the response is even though the header says 400. Is there a way to bypass this?
Heres the URL I’m attempting to fetch:
https://graph.facebook.com/me?access_token=your_token
First off all in my testing I find that the DownloadStringCompleted does fire.
However an attempt to read the event args
Resultproperty will throw an error. You need to test the event argsErrorproperty to determine if an error occured. If it has this property will hold a reference to aWebException.Fortunately the
WebExceptionhas aResponseobject which is of typeWebResponse. You can get the response string from this using a little function:-But there is a problem. This is only available when using the ClientHTTP stack not the BrowserHTTP stack. So you need something like this line of code in your app:-
So then code like this will work:-
Oh but there is possibly another problem. I don’t know anything about the facebook API but if there is a dependency on cookies then by default the ClientHTTP stack doesn’t manage cookies itself. To handle cookies properly you need to assign a common
CookieContainerto eachHttpWebRequestused.WebClientdoesn’t allow you access to theWebRequestobject used so you’ll need to drop to usingWebRequest/WebResponsedirectly.