I have a function which acquires the client-side source code of a site, but sadly it's not an async function :/ So, I created an async version of the function, and it's returning unexpected results. Here's the safe and working one
public static string GetWebSource(Uri url)
{
WebClient client = new WebClient();
Stream stream = client.OpenRead(url);
StreamReader reader = new StreamReader(stream);
string source = reader.ReadToEnd();
stream.Close();
reader.Close();
return source;
}
It returns the source code as expected, cool, great, except it's not async so it messes up the UI. Here is the async version
public async static Task<string> GetWebSourceAsync(Uri url)
{
WebClient client = new WebClient();
Stream stream = await client.OpenReadTaskAsync(url);
StreamReader reader = new StreamReader(stream);
string source = await reader.ReadToEndAsync();
stream.Close();
reader.Close();
return source;
}
Except this function returns "System.Threading.Tasks.Task`1[System.String]" I googled for a solution and I found Microsoft solution, they don't know how to make good explanations or straight solutions so that was a fraud.
Here is my code to print the value out
string source = GetWebSourceAsync(new Uri("http://checkip.dyndns.org/")).ToString();
source = source.Replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "").Replace("</body></html>", "");
Console.Write(source);
So how do I make it display the string? .ToString() extension method doesn't seem to work :/ Regards, TuukkaX.