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 cookies – response stream and protocol version for response from server using HttpWebResponse class C#.net – vb.net.

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

In this simple article you will learn that how we can get cookies, response stream and protocol version associated with response from server using HttpWebResponse class C#.net – vb.net.

 Cookies:

If you want to get the number of cookies received from server in response of your request, for this purpose HttpWebResponse class provides us a property named Cookies. This property returns the object of CookiesCollection class. We can get cookies from this collection object.

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

C#

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

                    CookieCollection ck = myWebResponse.Cookies;

                    int count = ck.Count;

                    string str = count.ToString();

                    MessageBox.Show(str);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_cookies_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 ck As CookieCollection = myWebResponse.Cookies

                Dim count As Integer = ck.Count

                Dim str As String = count.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 get cookies received from server.

Response Stream:

If you want to get the response stream to read the body of the response of your request received from server, for this purpose HttpWebResponse class provides us a function named GetResponseStream (). This function returns the object of stream class. By using this object we can read the body.

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

C#

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

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

                    MessageBox.Show(rd.ReadToEnd());

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

 Private Sub btn_ResponseStream_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 rd As New StreamReader(myWebResponse.GetResponseStream())

                MessageBox.Show(rd.ReadToEnd())

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get response stream which reads the body of response.

Protocol Version:

If you want to get the protocol version which is used in the response, for this purpose we have a property of HttpWebRequest class named ProtocolVersion. This property returns object of Version class.

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

C#

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

                    Version vr = myWebResponse.ProtocolVersion;

                    MessageBox.Show(vr.ToString());

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_version_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 vr As Version = myWebResponse.ProtocolVersion

                MessageBox.Show(vr.ToString())

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get protocol version used for 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 cookies, response stream and protocol version associated with response from server using HttpWebResponse class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get content length – content type – content encoding and character set for server response using HttpWebResponse class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net