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
 

Make – Abort request with server - getting read write stream timeout and media type for request using HttpWebRequest class C#.net – vb.net.

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

In this simple article you will learn that how we can make and abort request with server and how we can get/set read write stream time out and media type for request.

 Make Request:

To make request with server we have to create a httpwebrequest object for this purpose we have a static method named create () which takes url as string and return object of httpwebrequest class. Then get a response from server using GetResponse () method of httpwebrequest class object.

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

C#

private void btn_connection_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("Request Made");

                    count++;

                }

            }

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

        }

VB

Private Sub btn_connection_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("Request Made")

                count += 1

            End If

        Catch ex As Exception

            MessageBox.Show("invalid entry")

        End Try

    End Sub

This is simple code to make request with server.

Get Stream Read Write Timeout:

To get/set the stream read write time out with server we have a property of HttpWebRequest class named TimeOut. This property takes and returns integer value. This sets or gets the timeout time in milliseconds.

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

C#

private void btn_timeout_Click(object sender, EventArgs e)

        {

            if (count != 0)

            {

                int time = myWebRequest.Timeout;

                string str = time.ToString();

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("FRIRST MAKE REQUEST");

        }

VB

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

        If count <> 0 Then

            Dim time As Integer = myWebRequest.Timeout

            Dim str As String = time.ToString()

            MessageBox.Show(str)

        Else

            MessageBox.Show("FRIRST MAKE REQUEST")

        End If

    End Sub

This is simple code to get/set timeout for read write stream.

Get Media Type:

To get/set the media type of the request we have a property of HttpWebRequest class named MediaType. This property takes and returns a string.

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

C#

private void btn_media_Click(object sender, EventArgs e)

        {

            if (count != 0)

            {

                myWebRequest.MediaType = "My Media";

                string str = myWebRequest.MediaType;

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("FRIRST MAKE REQUEST");

        }

VB

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

        If count <> 0 Then

            myWebRequest.MediaType = "My Media"

            Dim str As String = myWebRequest.MediaType

            MessageBox.Show(str)

        Else

            MessageBox.Show("FRIRST MAKE REQUEST")

        End If

    End Sub

This simple code gets the media type for request.

Abort Request:

If you want to abort request with server for this purpose we have a function of HttpWebRequest class named abort (). This function takes and returns nothing.

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

C#

private void btn_abort_Click(object sender, EventArgs e)

        {

            if (count != 0)

            {

                myWebRequest.Abort();

                count = 0;

                MessageBox.Show("Request Aborted");

            }

            else

                MessageBox.Show("FRIRST MAKE REQUEST");

        }

VB:

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

        If count <> 0 Then

            myWebRequest.Abort()

            count = 0

            MessageBox.Show("Request Aborted")

        Else

            MessageBox.Show("FRIRST MAKE REQUEST")

        End If

    End Sub

This is simple code to abort the request.

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 how we can get that server allow auto redirect, allow write stream buffering  and how we can get  request http header value and request  group name using HttpWebRequest class C#.net – vb.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get that server allow auto redirect – write stream buffering - get connection and group name using HttpWebRequest class C#.net – vb.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net