I am trying to get the source code of a site. In windows application a simple http request would be enough. However in windows phone it is a lot more complicated. I searched
I finally found the correct answer to my question: Big thanks to @Peter Torr - MSFT for the help that leaded me to the exact answer of my problem
Answer
public static sReturn = "";
public async Task _InetReadEx(string sUrl)
{
try
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(new Uri(sUrl));
response.EnsureSuccessStatusCode();
//sStatus = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;
sSource = await response.Content.ReadAsStringAsync();
sSource = sSource.Replace("<br>", Environment.NewLine); // Insert new lines
}
catch (Exception hre)
{
sSource = string.Empty;
}
}
and the way to call it:
public MainPage()
{
InitializeComponent();
Task.Run(async () => { await _InetReadEx("http://url.com/"); }).Wait();
}
Big thanks to everyone and the community!
The problem is that you return sReturn
immediately, but the download won't complete until some time in the future. So sReturn
still has the default value of the empty string at the time you return it.
You can download this sample which includes code for doing exactly what you want to do using the HttpClient
portable library.