Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Get html source of web page using HttpWebRequest class C#.net – vb.net.

Author: Shehzad Hemani
Download Source Code : 994_HtmlDocument.zip

In this simple article you will learn that how we can get html source of a webpage using HttpWebRequest class in VB and C#.

 This article tells that how we can get html page source of a page just giving the url of that page. For this purpose we can use HttpWebRequest class provided by .net. This class sends the request to the webserver of the website and gets response from server by using HttpWebResponse class then reads the html code from that response.

Create HttpWebRequest Object:

To create object we have a static method of httpwebrequest class named create (). This method takes a string value which is the url of a page n returns the object of HttpWebRequest class.

C#

string url = "http://www.hotmail.com";

HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

VB

Dim url As String = "http://www.hotmail.com"

Dim myWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)

 

Get Method:

We have a static property named Method. This property decides that request is get or post. This property takes n returns string value which decides that nature of request that it is get or post.

C#

myWebRequest.Method = "GET";

VB

myWebRequest.Method = "GET"

Create HttpWebResponse Object:

To create object we have a httpwebrequest method named GetResponse (). This method takes nothing n returns the object of HttpWebResponse class.

C#

HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

VB

Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)

 

Get Html Source:

To get the html code of the page first of all you will have to create an object of HttpWebRequest class then you have to set method property to “Get”. So that it will tell the webserver that it is a get request. Then you will declare an object of httpwebresponse class using GetResponse () method of HttpWebRequest class. After creating object declare stream reader object and read the html source.

To demonstrate make a window application. Drag one text box, one button and one rich box on form. Write url in text box, press button and get source in rich box.

Now write the following code on Button click event:

C#

private void btn_Html_Click(object sender, EventArgs e)

        {

            string url=string.Empty;

            url=txt_url.Text;

            if (url ==string.Empty)

            {

                MessageBox.Show("Enter url");

            }

            else

            {

                try

                {

                    string pagesource = getHtml(url);

                    richTextBox1.Text = pagesource;

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Error In retreiving code");

                }

            }

        }

VB

Private Sub btn_Html_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim url As String = String.Empty

        url = txt_url.Text

        If url = String.Empty Then

            MessageBox.Show("Enter url")

        Else

            Try

                Dim pagesource As String = getHtml(url)

                richTextBox1.Text = pagesource

            Catch ex As Exception

                MessageBox.Show("Error In retreiving code")

            End Try

        End If

    End Sub

Now write the following code for getHtml (string) method.

C#

string getHtml(string url)

{

HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

myWebRequest.Method = "GET";

  // make request for web page

HttpWebResponse myWebResponse =

HttpWebResponse)myWebRequest.GetResponse();

StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());

string myPageSource = string.Empty;

myPageSource= myWebSource.ReadToEnd();

myWebResponse.Close();

return myPageSource;

 }

VB

Private Function getHtml(ByVal url As String) As String

 Dim myWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)

myWebRequest.Method = "GET"

     ' make request for web page

Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)

Dim myWebSource As New StreamReader(myWebResponse.GetResponseStream())

Dim myPageSource As String = String.Empty

myPageSource = myWebSource.ReadToEnd()

myWebResponse.Close()

Return myPageSource

    End Function

This is simple code to get the html source code of the web page.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP HTML SOURCE APPLICATION";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP HTML SOURCE APPLICATION"

    End Sub

This simple article tells how we can get html source of a webpage using HttpWebRequest class in VB and C#.

Article Comments
Very nice site!

Posted on 3/7/2010 1:49:10 AM by Very nice site!

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Check privileges – size - synchronization and synchronized root of the list dictionary collection using ListDictionary class vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net