WebClient alternative for Windows 8?

I use WebClientto get Yahoo data for Windows Phone 8 and Android HttpClient With WebClient I can do

 WebClient client = new WebClient();
   client.DownloadStringCompleted += new     DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    client.DownloadStringAsync(url);

after sending the event;

   StringReader stream = new StringReader(e.Result)

   XmlReader reader = XmlReader.Create(stream);
   reader.ReadToFollowing("yweather:atmosphere");
   string humidty = reader.MoveToAttribute("humidity");

but in Windows 8 RT there is no such thing.

How can I get the following data? > http://weather.yahooapis.com/forecastrss?w=2343732&u=c

+5
source share
1 answer

You can use the HttpClient class, something like this:

public async static Task<string> GetHttpResponse(string url)
{
    var request = new HttpRequestMessage(HttpMethod.Get, url);
    request.Headers.Add("UserAgent", "Windows 8 app client");

    var client = new HttpClient();
    var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

    if (response.IsSuccessStatusCode)
      return await response.Content.ReadAsStringAsync();
    else
     throw new Exception("Error connecting to " + url +" ! Status: " + response.StatusCode);
}

A simplified version will be simple:

public async static Task<string> GetHttpResponse(string url)
{
    var client = new HttpClient();
    return await client.GetStringAsync(url);
}

But if an HTTP error occurs, GetStringAsync will throw an HttpResponseException, and as far as I can see, there is no http status, except for the exception message.

UPDATE: , RSS-, HttpClient XML, SyndicationFeed, :

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh452994.aspx

+8

All Articles