加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – HttpWebRequest和WebClient在Windows Phone

发布时间:2020-12-14 05:28:22 所属栏目:Windows 来源:网络整理
导读:我正在尝试从 Windows Phone 7应用程序从此URL https://valueboxtest.lb.dk/mobile/categories下载常规JSON字符串. 我曾尝试过使用WebClient和HttpWebRequest.他们都抛出异常 “The remote server returned an error: NotFound” 这是使用WebClient的代码 va
我正在尝试从 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证书方面,移动设备更严格.

如果你想将这个应用程序带入生产环境,你需要为这个服务器编写一个包装器(如果它不是你自己的),或者获得一个有效的证书.在短期内,为了进行测试,您可以在设备中添加证书.

Here’s a tool which might help you install a certificate.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读