18

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?

sarvesh
  • 2,743
  • 1
  • 20
  • 30
NicoMinsk
  • 1,716
  • 6
  • 28
  • 53
  • 7
    By "windows8", do you actually mean "WinRT"? It should work absolutely fine on Windows 8 targeting the regular .NET framework. If you are targeting WinRT, then sure: you need to use what WinRT offers. – Marc Gravell Feb 28 '12 at 13:02
  • 2
    1. It's not a namespace, it's a class. 2. Yes, it's absent from WinRT as it encourages a bad practice of doing synchronous calls. Use HttpWebRequest class instead. – wizzard0 Feb 28 '12 at 13:36
  • 4
    "Yes, it's absent from WinRT as it encourages a bad practice of doing synchronous calls." This certainly is NOT the reason WebClient does not exist in WinRT because WebClient has Async versions most of its methods. The reason its missing from WinRT is likely because of other reasons. – Security Hound Feb 28 '12 at 14:32

1 Answers1

27

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.

sarvesh
  • 2,743
  • 1
  • 20
  • 30