I’m developing a client on WP7 that supports streaming and the contents are on a Rails server.
When I call a valid stream, everything works great but when I call a not valid stream, the server catches a WebException with the message:
The remote server returned an error: NotFound.
This is kind of weird because if I do the same request on a browser (pe. google chrome) I get the following json message:
{ "success":false, "error_code": "stream_not_allowed","error_message": "Streaming is not allowed" }
Well, I want to retrieve the error_message to display it to the client.
The code I’m using is:
private static void DoRequest(string path, GenericServiceHelper helper) {
var uri = new Uri(path);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
helper.request = request;
request.BeginGetResponse(HandleResponse, helper);
}
private static void HandleResponse(IAsyncResult ar) {
var helper = (GenericServiceHelper)ar.AsyncState;
HttpWebRequest originalRequest = helper.request;
HttpWebResponse response;
try {
response = (HttpWebResponse)originalRequest.EndGetResponse(ar);
}
catch (WebException e) {
// It fails here
}
// Do normal stuff
}
I believe you’ll get a
WebExceptionfor any non-success HTTP status code (which is pretty reasonable, IMO).You can get the response within a
WebExceptionvia theResponseproperty: