Senin, 20 Desember 2010

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

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

http://www.devasp.net/SourceCode/994_HtmlDocument.zip

Tidak ada komentar:

Posting Komentar