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 content length – content type – content encoding and character set for server response using HttpWebResponse class C#.net – vb.net.

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

In this simple article you will learn that how we can get content length, content type, content encoding method and character set associated with response from server using HttpWebResponse class.

 Content Length:

If you want to get the length of contents received from server in response of your request for this purpose HttpWebResponse class provides us a property named ContentLength. This property returns the long type value. This value is length of contents.

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

C#

private void btn_contentlength_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();

                    long length = myWebResponse.ContentLength;

                    string str = length.ToString();

                    MessageBox.Show(str);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_contentlength_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 length As Long = myWebResponse.ContentLength

                Dim str As String = length.ToString()

                MessageBox.Show(str)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to length of the contents received from server.

Content Type:

If you want to get the type of contents received from server in response of your request for this purpose HttpWebResponse class provides us a property named ContentType. This property returns the string type value.

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

C#

private void btn_contenttype_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 str = myWebResponse.ContentType;

                    MessageBox.Show(str);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

 Private Sub btn_contenttype_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 str As String = myWebResponse.ContentType

                MessageBox.Show(str)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get content type.

Character Set:

If you want to get the character set of response of your request for this purpose HttpWebResponse class provides us a property named CharacterSet. This property returns the string type value.

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

C#

private void btn_charset_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 str = myWebResponse.CharacterSet;

                    MessageBox.Show(str);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_charset_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 str As String = myWebResponse.CharacterSet

                MessageBox.Show(str)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This simple code gets character set of response.

Encoding Content:

If you want to get that which encoding method is used to encode the response, for this purpose we have a property of HttpWebRequest class named ContentEncoding. This property returns string value.

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

C#

private void btn_contentencoding_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 str = myWebResponse.ContentEncoding;

                    MessageBox.Show(str);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_contentencoding_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 str As String = myWebResponse.ContentEncoding

                MessageBox.Show(str)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get content encoding method.

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 content length, content type, content encoding method and character set associated with response from server using HttpWebResponse class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get Maiximum autometic redirections – response header length – authentication level and decompression methods using HttpWebRequest class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net