c# - Download string from URL using a portable class library (PCL) -


i'm trying download string webpage within portable class library. i've created basic setup:

  • created new pcl project
    • compatible wp8 , winrt compulsory components such silverlight

as webclient not compatible across these systems, not possible use:

string data = new webclient().downloadstring(); 

i've tried using (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 call second set of code external c# application referencing pcl, debugger fails no warning or error message on:

request.getresponseasync(); 

is there easy way download string i'm missing?

*also, why debugger exit no explanation?

edit:

here method have attempted - based on answer provided. again, method exits , force closes 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; } 

install nuget packages:

  • microsoft.bcl.async, adds async/await support pcls.
  • microsoft.net.http, adds httpclient support pcls.

then can easy way:

var client = new httpclient(); var data = await client.getstringasync(url); 

Comments