Get and Forget Url using vb.net

129 Views Asked by At

I want to create get and forget Url using vb.net. Basically I will push an url(just get response from url, not showing in the browser/run in background) and forget. I want push 10 url at the same time when I the button clicked. Here's my code, but it still processing the url the url one by one.

Private Sub BtnHit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHit.Click
Dim startp As DateTime = New DateTime(CDate(BtnStartDate.Text.ToString).Year, CDate(BtnStartDate.Text.ToString).Month, CDate(BtnStartDate.Text.ToString).Day)
    Dim endp As DateTime = New DateTime(CDate(BtnEndDate.Text.ToString).Year, CDate(BtnEndDate.Text.ToString).Month, CDate(BtnEndDate.Text.ToString).Day)
    Dim CurrD As DateTime = startp

    'Call PostTo("http://localhost:8089/Process/GetDataRencanaLembur.aspx?sid=88665765746873243", "POST", "&NIP=000323971&START_DATE=18 Aug 2015&END_DATE=19 Aug 2015")

    While (CurrD <= endp)
        Call PostTo("http://" & My.Settings.UrlServer & "/Process/SubmitRencanaLembur.aspx?sid=" & ((DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString.Trim, "POST", "&NOREG=" & Noreg.Trim & "&Nama=" & LblNama.Text.Trim & "&DirectReport=" & LblDirectReport.Text.Trim & "&Tanggal=" & Format(CurrD, "dd MMM yyyy") & "&OTPlan=03%3A00&Reason=Robot Testing")
        CurrD = CurrD.AddDays(1)
    End While
end sub

Protected Sub PostTo(ByVal url As String, ByVal method As String, ByVal postData As String)
    CreateLog("Start Time : " & Now.ToString("HH:mm:ss.fff"))
    Dim stopwatch As Stopwatch = stopwatch.StartNew()
    Dim myWebRequest As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
    myWebRequest.Method = method
    Dim byteArray As Byte() = System.Text.Encoding.[Default].GetBytes(postData)
    myWebRequest.ContentType = "application/x-www-form-urlencoded"
    myWebRequest.ContentLength = byteArray.Length
    Dim dataStream As System.IO.Stream = myWebRequest.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
    LblStatus.Text = (DirectCast(myWebResponse, HttpWebResponse)).StatusCode & " " & (DirectCast(myWebResponse, HttpWebResponse)).StatusDescription
    dataStream = myWebResponse.GetResponseStream()
    Dim reader As New System.IO.StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    RichTextBox1.Text = RichTextBox1.Text & vbNewLine & responseFromServer + ":"
    reader.Close()
    dataStream.Close()
    myWebResponse.Close()
    CreateLog("End Time : " & Now.ToString("HH:mm:ss.fff"))
    CreateLog("Parameter : " & postData & vbNewLine & "Status Code : " & LblStatus.Text & " " & stopwatch.ElapsedMilliseconds & "ms" & vbNewLine & "URL : " & url & postData & vbNewLine & "Response From Server : " & responseFromServer & vbNewLine)
End Sub
1

There are 1 best solutions below

4
On

You need to create a new thread within your while loop:

While (CurrD <= endp)
    Task.Factory.StartNew(Sub()
            Call PostTo("http://" & My.Settings.UrlServer & "/Process/SubmitRencanaLembur.aspx?sid=" & ((DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString.Trim, "POST", "&NOREG=" & Noreg.Trim & "&Nama=" & LblNama.Text.Trim & "&DirectReport=" & LblDirectReport.Text.Trim & "&Tanggal=" & Format(CurrD, "dd MMM yyyy") & "&OTPlan=03%3A00&Reason=Robot Testing")
            CurrD = CurrD.AddDays(1)
    End Sub)      
End While