Windows 8中不存在WebClient类

Nic*_*nsk 18 c# webclient windows-8 windows-runtime visual-studio-2012

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

我使用WebClient类,但我不能将它用于Windows 8("错误:无法找到类型或命名空间").

我还能用什么?

你能给我一个代码样本吗?

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

sar*_*esh 27

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

在开发Metro时,与桌面版相比,.Net框架将受到限制.如果您看到名称空间未找到错误,通常是由于这一事实.MSDN上的此链接具有可用于metro的命名空间列表.