使用WebRequest VB .Net重新加载URL

时间:2014-12-13 01:45:56

标签: vb.net httpwebrequest

我在我的测试html页面上放置了在线计数器,当我尝试重新加载此页面时,计数器没有刷新,所以看起来这个WebRequest无法正常工作,感谢您的帮助,这就是我现在所得到的:

    Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sites() As String = New String() {"TextBox1.Text"}
                Debug.WriteLine(sites.Length)
        'Stop
        For Each s As String In sites
            Dim t As New Threading.Thread(AddressOf threadimportbtcUSD)
            t.IsBackground = True
            t.Start(s)
            Threading.Thread.Sleep(1000)
        Next
    End Sub

    Private Sub threadimportbtcUSD(s As Object)
        Dim asite As String = DirectCast(s, String)
        Do
            Debug.WriteLine(DateTime.Now.ToLongTimeString)
            Dim postReq As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(asite), Net.HttpWebRequest)

            Try
                postReq.Timeout = 5000
                postReq.ContentType = "application/x-www-form-urlencoded"
                postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
                Dim postresponse As Net.HttpWebResponse
                postresponse = DirectCast(postReq.GetResponse(), Net.HttpWebResponse)
                Dim risposta As String
                Dim postreqreader As New IO.StreamReader(postresponse.GetResponseStream())
                risposta = postreqreader.ReadToEnd
                postreqreader.Close()
                postreqreader.Dispose()

                postresponse.Close()
                postresponse.Dispose()
            Catch ex As Exception
                Debug.WriteLine(ex.ToString)
                postReq.Abort()
            End Try
            Threading.Thread.Sleep(1000)
        Loop
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

试试这个:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    doSubmitHTTPRequest(TextBox1.Text)
End Sub

Private Sub doSubmitHTTPRequest(ByVal url As String)
    Dim webRequest As Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url & "?nocache=" & DateTime.Now.Ticks), Net.HttpWebRequest)
    webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
    webRequest.Method = "GET"
    webRequest.ContentType = "text/xml; encoding='utf-8'"

    Dim responseReader As IO.StreamReader = New IO.StreamReader(webRequest.GetResponse().GetResponseStream())
    Dim responseData As String = responseReader.ReadToEnd()

    responseReader.Close()
    webRequest.GetResponse().Close()

    'responseData <= variable which contains the response from the server
End Sub