Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the Contents of a Web Page with C#

Tags:

dom

c#

.net

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

I know this is doable in C# with its web browser control. But I couldn't find a good, simple example of doing it. All the resources I found online involve creating Forms and GUI, which I don't need, I just need a good old Console Application.

If anyone can provide a simple console-based code snippet that accomplishes the above, it'll be greatly appreciated.

like image 670
Saobi Avatar asked Jul 14 '09 14:07

Saobi


People also ask

Can you use C on a website?

Yes, although most people won't use C anymore for web development... To use C for web development, you have two options. The first option, called Common Gateway Interface or CGI, is when you're already using some web server software like Apache or IIS.

How do you read the contents of a website?

To use Read Aloud, navigate to the web page you want to read, then click the Read Aloud icon on the Chrome menu. In addition, the shortcut keys ALT-P, ALT-O, ALT-Comma, and ALT-Period can be used to Play/Pause, Stop, Rewind, and Forward.


2 Answers

Actually the WebBrowser is a GUI control used in case you want to visualize a web page (embed and manage Internet Explorer in your windows application). If you just need to get the contents of a web page you could use the WebClient class:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            var contents = client.DownloadString("http://www.google.com");
            Console.WriteLine(contents);
        }
    }
}
like image 100
Darin Dimitrov Avatar answered Oct 27 '22 01:10

Darin Dimitrov


If you just want the content and not an actual browser, you can use an HttpWebRequest.

Here's a code sample: http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=58261

like image 29
Matthew Groves Avatar answered Oct 26 '22 23:10

Matthew Groves