How to retrieve a webpage with C#?

2019-03-12 05:28发布

问题:

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

回答1:

Use the System.Net.WebClient class.

System.Console.WriteLine(new System.Net.WebClient().DownloadString(url));


回答2:

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.



回答3:

// 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");


标签: c# http