How to retrieve a webpage with C#?

2019-03-12 04:37发布

How to retrieve a webpage and diplay the html to the console with C# ?

标签: c# http
3条回答
相关推荐>>
2楼-- · 2019-03-12 05:15

Use the System.Net.WebClient class.

System.Console.WriteLine(new System.Net.WebClient().DownloadString(url));
查看更多
孤傲高冷的网名
3楼-- · 2019-03-12 05:23
// Save HTML code to a local file.
WebClient client = new WebClient ();
client.DownloadFile("http://yoursite.com/page.html", @"C:\htmlFile.html");

// Without saving it.
string htmlCode = client.DownloadString("http://yoursite.com/page.html");
查看更多
乱世女痞
4楼-- · 2019-03-12 05:39

I have knocked up an example:

WebRequest r = WebRequest.Create("http://www.msn.com");
WebResponse resp = r.GetResponse();
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    Console.WriteLine(sr.ReadToEnd());
}

Console.ReadKey();

Here is another option, using the WebClient this time and do it asynchronously:

static void Main(string[] args)
{
    System.Net.WebClient c = new WebClient();
    c.DownloadDataCompleted += 
         new DownloadDataCompletedEventHandler(c_DownloadDataCompleted);
    c.DownloadDataAsync(new Uri("http://www.msn.com"));

    Console.ReadKey();
}

static void c_DownloadDataCompleted(object sender, 
                                    DownloadDataCompletedEventArgs e)
{
    Console.WriteLine(Encoding.ASCII.GetString(e.Result));
}

The second option is handy as it will not block the UI Thread, giving a better experience.

查看更多
登录 后发表回答