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 that is response from cache – is mutually authenticated and response uri from server using HttpWebResponse class C#.net – vb.net.

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

In this simple article you will learn that how we can check that response was from cache or not, server and client are authenticated or not and response uri from server using HttpWebResponse class.

 Is From Cache:

To check that response was from cache or not,for this purpose HttpWebResponse class provides us a property named IsFromCache. This property returns a Boolean value. If value is true means response was from cache else it was not from cache.

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

C#

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

                    bool b = myWebResponse.IsFromCache;

                    string result = b.ToString();

                    MessageBox.Show(result);

                }

                myWebRequest.Abort();

                myWebResponse.Close();

            }

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

        }

VB

Private Sub btn_cache_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 b As Boolean = myWebResponse.IsFromCache

                Dim result As String = b.ToString()

                MessageBox.Show(result)

            End If

            myWebRequest.Abort()

            myWebResponse.Close()

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to check that response was from cache or not.

Mutually Authenticated:

To check that server and client are mutually authenticated or not, for this purpose HttpWebResponse class provides us a property named IsMutuallyAuthenticated. This property returns a Boolean value. If value is true means server and client are authenticated else they are not.

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

C#

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

                    bool b = myWebResponse.IsMutuallyAuthenticated;

                    string result = b.ToString();

                    MessageBox.Show(result);

                }

                myWebRequest.Abort();

                myWebResponse.Close();

            }

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

        }

VB

Private Sub btn_authenticated_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 b As Boolean = myWebResponse.IsMutuallyAuthenticated

                Dim result As String = b.ToString()

                MessageBox.Show(result)

            End If

            myWebRequest.Abort()

            myWebResponse.Close()

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to check that server and client are authenticated or not.

Response Uri:

To get the actual uri from where you got the response, HttpWebResponse class provides us a property named ResponseUri. This property returns an object of Uri class.

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

C#

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

                    Uri ur = myWebResponse.ResponseUri;

                    MessageBox.Show(ur.ToString());

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_responseuri_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 Uri = myWebResponse.ResponseUri

                MessageBox.Show(ur.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 the server uri from where 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 check that response was from cache or not, server and client are authenticated or not and response uri from server using HttpWebResponse class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get – Set Cache policy – Impersonation level and authenticate header using HttpWebRequest class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net