WebClient class doesn't exist in Windows 8

这一生的挚爱 提交于 2019-12-17 18:37:44

问题


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

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

What else can I use?

Can you provide me a sample of code?

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


回答1:


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();
}

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

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.
}

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.



来源:https://stackoverflow.com/questions/9482402/webclient-class-doesnt-exist-in-windows-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!