Access web page content using C #

I am trying to use C # to access the contents of a webpage. For example, I want to capture the body text of a google homepage.

I know this is possible in C # with its web browser control. But I could not find a good, simple example of this. All the resources that I found on the Internet include creating forms and a GUI that I don’t need, I just need a good old console application.

If someone can provide a simple console-based code snippet that does the above, we will be very grateful.

+5
source share
6 answers

, WebBrowser - , , - ( Internet Explorer Windows). -, WebClient:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            var contents = client.DownloadString("http://www.google.com");
            Console.WriteLine(contents);
        }
    }
}
+12

- :

Uri u = new Uri( @"http://launcher.worldofwarcraft.com/alert" );
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(u);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
System.IO.Stream st = res.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(st);
string body = sr.ReadToEnd();
System.Console.WriteLine( "{0}", body ); 

WoW USA ( - )

+1

WatiN -. -. , http://watin.sourceforge.net/. # HTML- Google ( WatiN). , .

   using System;
    using WatiN.Core;

    namespace Test
    {
      class WatiNConsoleExample
      {
        [STAThread]
        static void Main(string[] args)
        {
          // Open an new Internet Explorer Window and
          // goto the google website.
          IE ie = new IE("http://www.google.com");

          // Write out the HTML text of the body
          Console.WriteLine(ie.Text);


          // Close Internet Explorer and the console window immediately.
          ie.Close();

          Console.Readkey();
        }
      }
    } 
+1

HTML Agility Pack , . HTML- DOM XPath.

0

Google Screenshot and as mentioned above use HttpWebRequest. When you do what you do, I would recommend using Fiddler to help you understand what is really going on.

0
source

All Articles