Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27020

speeding up the HttpWebRequest POST speed

$
0
0
I have here a working HttpWebRequest code using POST method, but my problem is it is very slow. Sends a request every 1 second interval. Is there any way to speed this up?

Here is my code:

Code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try
        If BackgroundWorker1.IsBusy <> True Then
            BackgroundWorker1.RunWorkerAsync()             
        End If

    Catch ex As Exception

    End Try

End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim x As Integer = 0
    Do
        Try
            Dim cweb As String = RichTextBox1.Text
            Dim POST As String = RichTextBox2.Text
            Dim request As HttpWebRequest
            Dim response As HttpWebResponse

            request = CType(WebRequest.Create(cweb), HttpWebRequest)
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"
            request.AllowAutoRedirect = False
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = POST.Length
            request.Method = "POST"
            request.KeepAlive = False

            Dim requestStream As Stream = request.GetRequestStream()
            Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
            requestStream.Write(postBytes, 0, postBytes.Length)
            requestStream.Close()

            response = CType(request.GetResponse(), HttpWebResponse)
            response.Close()

            x += 1
            Label5.Text = x

        Catch ex As Exception
            MessageBox.Show("failed")
        End Try
    Loop While (x < 100)

End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Try

    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Try
    Catch ex As Exception

    End Try
End Sub

I want to optimize it's speed similar to a normal loop condition below:

Code:

Dim x As Integer = 0

Do

x+=1

Loop While(x< 100)

Is this possible? Or are there any alternative ways in achieving this? Thanks in advance.

Viewing all articles
Browse latest Browse all 27020

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>