使用 vb.net 获取和忘记 URL

Posted

技术标签:

【中文标题】使用 vb.net 获取和忘记 URL【英文标题】:Get and Forget Url using vb.net 【发布时间】:2015-11-17 09:50:58 【问题描述】:

我想使用 vb.net 创建 get and forget Url。基本上我会推送一个 url(只是从 url 获得响应,不在浏览器中显示/在后台运行)然后忘记。 我想在单击按钮时同时推送 10 个 url。 这是我的代码,但它仍然会一一处理url。

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】:

您需要在 while 循环中创建一个新线程:

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

【讨论】:

你能帮帮我吗,给我一个例子,如何在我的代码中实现线程代码 只需将您的 while 循环替换为我发布的代码即可。 但尝试导入 System.Threading.Tasks(.net 4.5 版)时出现错误 命名空间“Imports System.Threading.Tasks”包含任何公共成员或找不到。

以上是关于使用 vb.net 获取和忘记 URL的主要内容,如果未能解决你的问题,请参考以下文章

用Webbrowser怎么实现获取网页内容并自动点击?(VB.NET)

在vb.net中使用Iphlpapi.dll获取网络信息

在堆栈跟踪中获取 VB.NET 行号

获取两个标签之间的内部文本 - VB.NET - HtmlAgilityPack

如何使用 vb.net 从 maxmind GeoIP ISP.data 获取 ISP

如何使用 C# 或 VB.NET 获取当前的本地主机名? [复制]