我正在尝试从
Windows Phone 7应用程序从此URL
https://valueboxtest.lb.dk/mobile/categories下载常规JSON字符串.
我曾尝试过使用WebClient和HttpWebRequest.他们都抛出异常
“The remote server returned an error: NotFound”
这是使用WebClient的代码
var webClient = new WebClient(); webClient.DownloadStringCompleted += (client_DownloadStringCompleted); webClient.DownloadStringAsync(new Uri("https://valueBoxtest.lb.dk/mobile/categories"));
然后,eventhandler只显示内容,但e.Result抛出上述异常:
void client_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e) { if (e.Error == null && !e.Cancelled) MessageBox.Show(e.Result); }
对于HttpWebRequest,我的代码如下所示:
var httpReq = (HttpWebRequest)WebRequest.Create(new Uri("https://valueBoxtest.lb.dk/mobile/categories")); httpReq.BeginGetResponse(HTTPWebRequestCallBack,httpReq);
通过以下回调:
private void HTTPWebRequestCallBack(IAsyncResult result) { var httpRequest = (HttpWebRequest)result.AsyncState; var response = httpRequest.EndGetResponse(result); var stream = response.GetResponseStream(); var reader = new StreamReader(stream); this.Dispatcher.BeginInvoke( new delegateUpdate(update),new Object[] { reader.ReadToEnd() } ); }
并使用委托方法
delegate void delegateUpdate(string content); private void update(string content) { MessageBox.Show(content); }
在控制台应用程序中运行它
一切正常,JSON字符串返回没有问题,我可以将结果打印到控制台.
不同的URL适用于WP7
奇怪的是,URL http://mobiforge.com/rssfeed实际上在上述两种情况下都能正常工作.
在模拟器和实际设备上都会出现此问题.
可能有什么不对? REST服务是否以不正常的方式返回数据?我真的希望你能帮助我!
注意:我没有同时运行Fiddler2!
解决方法
原因是因为该站点没有有效的证书.只需在Mobile Internet Explorer上试用它,您就会得到有关证书问题的提示.
How to ignore SSL certificates
在SSL证书方面,移动设备更严格.
如果你想将这个应用程序带入生产环境,你需要为这个服务器编写一个包装器(如果它不是你自己的),或者获得一个有效的证书.在短期内,为了进行测试,您可以在设备中添加证书.