hope somebody can help me with my problem.
I am trying to get the html code of a web site using the following code:
public string DownloadString(string add)
{
string html = "";
using (WebClient client = new WebClient())
{
client.Proxy = null;
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
while (html == "")
{
try
{
html = client.DownloadString(add);
}
catch (WebException e)
{
html = "";
}
}
client.Dispose();
}
return html;
}
And I need the string in this function(Caller):
public HtmlNode get_html(string add)
{
add_val(add);
Uri madd = new Uri(add);
Stopwatch timer = Stopwatch.StartNew();
Task<string> task = Task.Factory.StartNew<string>
(() => DownloadString(add));
string html = task.Result;
//string html = DownloadString(add);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
//doc.Load(new StringReader(html));
doc.LoadHtml(html);
HtmlNode root = doc.DocumentNode;
timer.Stop();
TimeSpan timespan = timer.Elapsed;
label18.Text = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);
return root;
}
I have tried html = await client.DownloadStringTaskAsync(new Uri(add));
but it doesn't seem to work, it still freezes the UI when i download a string.
Thank you in advance!