I would like to be able to read the html source of a certain webpage into a string in c# using winforms
how do I do this?
string html = new WebClient().DownloadString("http://twitter.com");
And now with async/await hotness in C# 5
string html = await new WebClient().DownloadStringTaskAsync("http://github.com");
Have a look at WebClient.DownloadString:
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString(address);
}
You can use WebClient.DownloadStringAsync or a BackgroundWorker to download the file without blocking the UI.
var req = WebRequest.Create("http://www.dannythorpe.com");
req.BeginGetResponse(r =>
{
var response = req.EndGetResponse(r);
var stream = response.GetResponseStream();
var reader = new StreamReader(stream, true);
var str = reader.ReadToEnd();
Console.WriteLine(str);
}, null);
精彩评论