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 responded contents last modified time – response status code and status description using HttpWebResponse class C#.net – vb.net.

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

In this simple article you will learn that how we can get last modified time of contents sent by server in response of your request, response status code and status description using HttpWebResponse.

 Last Modified Time:

If you want to get the last modified date and time of contents which was sent to you by server, against the response of your request,for this purpose HttpWebResponse class provides us a property named LastModified. This property returns an object of class DateTime.

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

C#

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

                    DateTime dt = myWebResponse.LastModified;

                    string result = dt.ToLongDateString() + dt.ToLongTimeString();

                    MessageBox.Show(result);

                }

                myWebRequest.Abort();

                myWebResponse.Close();

            }

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

        }

VB

Private Sub btn_modified_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 dt As DateTime = myWebResponse.LastModified

                Dim result As String = dt.ToLongDateString() + dt.ToLongTimeString()

                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 get last modified time of contents.

Response Status Code:

To get the status of the response, HttpWebResponse class provides us a property named StatusCode. This property returns an object of HttpStatusCode class.

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

C#

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

                    HttpStatusCode st = myWebResponse.StatusCode;

                    string result = st.ToString();

                    MessageBox.Show(result);

                }

                myWebRequest.Abort();

                myWebResponse.Close();

            }

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

        }

VB

Private Sub btn_statuscode_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 st As HttpStatusCode = myWebResponse.StatusCode

                Dim result As String = st.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 get the status of response.

Response Status Description:

To get the status description of the response, HttpWebResponse class provides us a property named StatusDescription. This property returns a string value.

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

C#

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

                    MessageBox.Show(myWebResponse.StatusDescription);

                    myWebRequest.Abort();

                    myWebResponse.Close();

                }

            }

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

        }

VB

Private Sub btn_statusdescrption_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)

                MessageBox.Show(myWebResponse.StatusDescription)

                myWebRequest.Abort()

                myWebResponse.Close()

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to get status description associated with 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 last modified time of contents sent by server in response of your request, response status code and status description using HttpWebResponse class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get that is response from cache – is mutually authenticated and response uri from server using HttpWebResponse class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net