开发者

c#: reading html source of a webpage into a string [duplicate]

开发者 https://www.devze.com 2022-12-26 19:40 出处:网络
This que开发者_运维技巧stion already has answers here: How can I download HTML source in C# (5 answers)
This que开发者_运维技巧stion already has answers here: How can I download HTML source in C# (5 answers) Closed 5 years ago.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消