Download string from URL using a portable class library (PCL)
Asked Answered
S

2

5

I'm trying to download a string from ANY webpage within my portable class library. I've created the most basic setup:

  • created a new PCL project
    • compatible with WP8 and WinRT as well as the compulsory components such as Silverlight

As WebClient is not compatible across these systems, it is not possible to use:

string data = new WebClient().DownloadString();

I've tried using this as well (uses this):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = HttpMethod.Get;
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

string data = ""
using (var sr = new StreamReader(response.GetResponseStream()))
{
    data = sr.ReadToEnd();
}

However, when I call the second set of code from an external C# application referencing the PCL, the debugger simply fails with NO warning or error message on:

request.GetResponseAsync();

Is there an easy way to download a string that I'm missing?

*also, why would the debugger simply exit with no explanation?

Edit:

Here is another method I have attempted - based on an answer already provided. Again, this method simply exits and force closes the debugger.

PCL Method:

public static async Task<string> DownloadString()
{
    var url = "http://google.com";
    var client = new HttpClient();
    var data = await client.GetStringAsync(url);

    return data;
}

Calling method:

private static async void Method()
{
    string data = await PCLProject.Class1.DownloadString();
    return data;
}
Sublapsarianism answered 19/7, 2013 at 15:11 Comment(1)
Are you running this in a desktop app, Windows Store app, WP8 app, or what when you see this behavior? Are you sure you are awaiting or waiting on all the tasks? (Otherwise the program may simply run to completion and exit before the download finishes.) If you're on Windows Phone, are you doing anything that would block the UI thread on the completion of the download (ie via Task.Wait or Task.Result)?Chronological
S
21

Install the NuGet packages:

  • Microsoft.Bcl.Async, which adds async/await support to PCLs.
  • Microsoft.Net.Http, which adds HttpClient support to PCLs.

Then you can do it the easy way:

var client = new HttpClient();
var data = await client.GetStringAsync(url);
Sollars answered 19/7, 2013 at 16:26 Comment(3)
I'm still having the same problem. Even after I created two new, separate projects - the method simply fails with no warning or exception. I will edit my original post to include this attempt.Sublapsarianism
Make sure your firewall is letting your app through. Also, make sure you're hitting a DNS.Motet
Why in the world would my firewall be interfering?Sublapsarianism
K
1

This method worked for me, it returned the HTML source code from google.com:

public async void GetStringFromWebpage()
{
    using (HttpClient wc = new HttpClient())
    {
      var data = await wc.GetStringAsync("http://google.com/");
      Debug.WriteLine("string:" + data);
    }
}
Kv answered 13/9, 2016 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.