我正在WP7上开发一个支持流媒体的客户端,内容在Rails服务器上。
当我调用有效的流时,一切都很好但是当我调用一个无效的流时,服务器会捕获WebException
并显示以下消息:
The remote server returned an error: NotFound.
这有点奇怪,因为如果我在浏览器上执行相同的请求(pe.google chrome),我会收到以下json消息:
{ "success":false, "error_code": "stream_not_allowed","error_message": "Streaming is not allowed" }
好吧,我想检索error_message以将其显示给客户端。
我正在使用的代码是:
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
}
答案 0 :(得分:2)
我相信你会得到WebException
任何不成功的HTTP状态代码(这是非常合理的,IMO)。
您可以通过Response
属性在WebException
内获得回复:
catch (WebException e) {
// You should check the status code etc to make sure you're not masking
// real errors.
response = e.Response;
}