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 Headers – Specific Header Contents and server name for response from server using HttpWebResponse class C#.net – vb.net.

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

In this simple article you will learn that how we can get headers, contents of a specific header and server name associated with response from server using HttpWebResponse class C#.net – vb.net.

 Headers:

To get the headers which are associated with the response sent from server, HttpWebResponse class provides us a property named Headers. This property returns the WebHeaderCollection class object. We can get headers by using this object.

To demonstrate make a window application. Drag one text box and one button on form.

C#

private void btn_headers_Click(object sender, EventArgs e)

        {

            try

            {

                url = txt_url.Text;

                if (url == string.Empty)

                {

                    MessageBox.Show("Enter valid url");

                }

                else

                {

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

                    myWebRequest.Method = "GET";

                    myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

                    WebHeaderCollection col = myWebResponse.Headers;

                    for (int i = 0; i < col.Count; i++)

                        MessageBox.Show(col.Get(i));

                }

                myWebRequest.Abort();

                myWebResponse.Close();

            }

            catch (Exception ex) { MessageBox.Show("invalid entry"); }

        }

VB

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

        Try

            url = txt_url.Text

            If url = String.Empty Then

                MessageBox.Show("Enter valid url")

            Else

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

                myWebRequest.Method = "GET"

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

                Dim col As WebHeaderCollection = myWebResponse.Headers

                For i As Integer = 0 To col.Count - 1

                    MessageBox.Show(col.[Get](i))

                Next

            End If

            myWebRequest.Abort()

            myWebResponse.Close()

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get headers associated with response.

Get Header Content:

To get the contents of a specific header associated with the response sent from server, HttpWebResponse class provides us a function named GetResponseHeader (). This function takes a string value as parameter which is header name and also returns string value which is the contents of the header.

To demonstrate make a window application. Drag one text box and one button on form.

C#

private void btn_Heardercontent_Click(object sender, EventArgs e)

        {

            try

            {

                url = txt_url.Text;

                if (url == string.Empty)

                {

                    MessageBox.Show("Enter valid url");

                }

                else

                {

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

                    myWebRequest.Method = "GET";

                    myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

                    WebHeaderCollection col = myWebResponse.Headers;

                    for (int i = 0; i < col.Count; i++)

                    {

                        string headername = col.Get(i);

                        MessageBox.Show(headername);

                        string headercontent = myWebResponse.GetResponseHeader(headername);

                        MessageBox.Show(headercontent);

                    }

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

            catch (Exception ex) { MessageBox.Show("invalid entry"); }

        }

VB

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

        Try

            url = txt_url.Text

            If url = String.Empty Then

                MessageBox.Show("Enter valid url")

            Else

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

                myWebRequest.Method = "GET"

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

                Dim col As WebHeaderCollection = myWebResponse.Headers

                For i As Integer = 0 To col.Count - 1

                    Dim headername As String = col.[Get](i)

                    MessageBox.Show(headername)

                    Dim headercontent As String = myWebResponse.GetResponseHeader(headername)

                    MessageBox.Show(headercontent)

                Next

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get contents of a specific header.

Get Server Name:

To get the web server name from which you got the response, HttpWebResponse class provides us a property named Server. This property returns a string value which contains the name of the web server.

To demonstrate make a window application. Drag one text box and one button on form.

C#

private void btn_server_Click(object sender, EventArgs e)

        {

            try

            {

                url = txt_url.Text;

                if (url == string.Empty)

                {

                    MessageBox.Show("Enter valid url");

                }

                else

                {

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

                    myWebRequest.Method = "GET";

                    myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

                    string ur = myWebResponse.Server;

                    MessageBox.Show(ur);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

            catch (Exception ex) { MessageBox.Show("invalid entry"); }

        }

VB

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

        Try

            url = txt_url.Text

            If url = String.Empty Then

                MessageBox.Show("Enter valid url")

            Else

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

                myWebRequest.Method = "GET"

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

                Dim ur As String = myWebResponse.Server

                MessageBox.Show(ur)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get the server name from which you got the response.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP APPLICATION";

        }

VB

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

        Me.Text = "DEVASP APPLICATION"

    End Sub

This simple article tells that how we can get headers, contents of a specific header and server name associated with response from server using HttpWebResponse class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get responded contents last modified time – response status code and status description using HttpWebResponse class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net