1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

speeding up the HttpWebRequest POST speed

Discussion in 'Visual Basic .NET' started by VBLearner, Sep 19, 2013.

  1. VBLearner

    VBLearner Newbie

    Joined:
    Sep 19, 2013
    Messages:
    6
    Likes Received:
    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[COLOR=#333333]
    
    [/COLOR]


    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)[COLOR=#333333]
    [/COLOR]


    Is this possible? Or are there any alternative ways in achieving this? Thanks in advance.
     
    Last edited: Sep 19, 2013
  2. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    8,844
    Likes Received:
    7,452
    Occupation:
    ZLinky2Buy SEO Services
    Location:
    ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩
    Home Page:
    The best way to speed up VB is to use C++ ;)
     
  3. VBLearner

    VBLearner Newbie

    Joined:
    Sep 19, 2013
    Messages:
    6
    Likes Received:
    0
    is making HttpWebRequest in C++ faster?
    I have not tried. But i have spent 2 months for this project already. I don't want to start from scratch.
     
  4. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    8,844
    Likes Received:
    7,452
    Occupation:
    ZLinky2Buy SEO Services
    Location:
    ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩
    Home Page:
    I was just kidding. I don't use VB and can't help you. But yes, C++ is scores faster in any similar function.
     
  5. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    The only way to speed things up is to group your requests and don't do one every second. For 1 second it can't connect send the request, receive the response and close. Also you will end up overloading the receiver of the request. Rethink the project and lower/qroup the requests
     
  6. VBLearner

    VBLearner Newbie

    Joined:
    Sep 19, 2013
    Messages:
    6
    Likes Received:
    0
    Does it mean i have to create multiple instances of requests all at once? I really want to get rid of this delay.
    What changes should I have to do with my code?
     
  7. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    no, i meant to combine the data you send in each request into one whole and do just 1 request
     
  8. VBLearner

    VBLearner Newbie

    Joined:
    Sep 19, 2013
    Messages:
    6
    Likes Received:
    0
    I won't be able to do that as the purpose of my request is to fill-up a webform and submit it. I need to process the data one by one.
     
  9. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    well, in that case you need multithreading. Head to google and dig up about VB multithreading, there are tons of examples and code samples about it
     
  10. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    C++ won't be faster than VB, that's for sure. Speed depends on your internet connection, nothing more. You might want to set timeout.
     
    • Thanks Thanks x 1
  11. VBLearner

    VBLearner Newbie

    Joined:
    Sep 19, 2013
    Messages:
    6
    Likes Received:
    0
    Is there any other way to create webrequests that is not speed dependent on internet connection?
    So it will get rid of the delay. My main purpose is to create a spam bot that fills-up a webform and submit it simultaneously.
     
    Last edited: Sep 19, 2013
  12. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    you need to learn about multithreading. it wont speed up the reuqest, but you make 5 requests at once
     
  13. roach

    roach BANNED BANNED

    Joined:
    Sep 8, 2009
    Messages:
    740
    Likes Received:
    395
    Here is a very simple function using multithreading:

    Code:
    Private Sub StartThreads()
                    For i = 0 To 50
                    Dim thread = New Thread(AddressOf test)
                    thread.IsBackground = True
                    thread.Start()
                    Next
             End Sub
    That will start 50 threads that are background. Each thread will run the sub or function called test :

    Code:
    Public Sub Test()
                      MessageBox.show("You just started another thread")
               End Sub
    That is the most basic of basic multi-Threading Just make sure to use Thread.isBackground = True If you dont then when you exit your application you will still have threads running. :) well good luck!
     
  14. Herion

    Herion Jr. VIP Jr. VIP Premium Member

    Joined:
    Jul 8, 2012
    Messages:
    286
    Likes Received:
    72
    There is no way to create web requests that are not depended on your internet speed. If it is taking a second to process each request, then simply add multi-threading as suggested above. There is no other way to get noticeably better results.
     
    • Thanks Thanks x 1
  15. zx123

    zx123 Senior Member

    Joined:
    Feb 26, 2009
    Messages:
    1,158
    Likes Received:
    875
    Home Page:
    This is very simple and can cause a lot of I/O error ;)