Windows 8 中不存在 WebClient 类 [英] WebClient class doesn't exist in Windows 8

查看:33
本文介绍了Windows 8 中不存在 WebClient 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 HTTP 网络服务,并且我已经为 wp7 开发了一个应用程序.

I want to use a HTTP webservice, and I've already developed an app for wp7.

我使用 WebClient 类,但不能在 windows 8 上使用它(错误:找不到类型或命名空间").

I use the WebClient class, but I can not use it for windows 8 ("error: type or namespace can not be found").

我还能用什么?

你能给我一个代码示例吗?

Can you provide me a sample of code?

当命名空间不存在时,Microsoft 是否有网站可以提供帮助?

Does Microsoft have a site to help when a namespace don't exist?

推荐答案

选项 1:HttpClient 如果您不需要确定性进度通知,这就是您想要使用的.示例.

Option 1 : HttpClient if you don't need deterministic progress notification this is what you want use. Example.

public async Task<string> MakeWebRequest()
{
       HttpClient http = new System.Net.Http.HttpClient();
       HttpResponseMessage response = await http.GetAsync("http://www.example.com");
       return await response.Content.ReadAsStringAsync();
}

选项 2:当您需要进度通知时,您可以使用 DownloadOperationBackgroundDownloader.MSDN 上的这个 示例 是一个好的开始.

Option 2: When you need progress notifications you can use DownloadOperation or BackgroundDownloader. This sample on MSDN is a good start.

选项 3:由于您提到了 Web 服务,如果它返回 XML,您可以使用 XmlDocument.LoadFromUriAsync 将返回一个 XML 文档.示例

Option 3: Since you mentioned web service and if it is returning XML you can use XmlDocument.LoadFromUriAsync which will return you an XML document. Example

public async void DownloadXMLDocument()
{
      Uri uri = new Uri("http://example.com/sample.xml");
      XmlDocument xmlDocument = await XmlDocument.LoadFromUriAsync(uri);
      //do something with the xmlDocument.
}

当您为 Metro .Net 框架开发时,与桌面版本相比会受到限制.如果您看到 namespace not found 错误,通常是由于这个事实.此链接在 MSDN 上有可用于 Metro 的命名空间、类的列表.

When you are developing for metro .Net framework will be limited compared to the desktop version. If you see namespace not found error it is usually due to this fact. This link on the MSDN has the list of namespaces, classes available for metro.

这篇关于Windows 8 中不存在 WebClient 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆