我在ASP.net绝对初学者(VB)。请原谅我,如果这个问题是针对有经验的成员太明显了。
我试图做一个简单的WebRequest在情况下,目标URL需要较长提供的数据异步模式。 在下面我的代码,我只是想看看如果回调块(RespCallback)每一次都被正确调用。 如果一切顺利,lblResult应该有我点击它调用“的GetData”子键,每次追加到它的字符串“123”。
然而,lblResult只显示首次点击后,123。 随后点击后,lblResult仅获取与“12”附加,仿佛RespCallback不会被调用。 当我试图在Visual Studio调试此,执行真正加强对进入RespCallback部分和lblResult.Text看实际显示“123123”,但所产生的网页始终只显示“12312”
我相信,我失去了一些东西基本在这里,但我只是不知道是什么。 我甚至猜测,它与浏览器缓存(因此第二次结果的变化)这样做,但我不知道如何解决,要么。
是否有人可以帮助? 提前致谢。
吉姆
Dim myWebRequest As WebRequest
Public Shared allDone As New ManualResetEvent(False)
Private Shared BUFFER_SIZE As Integer = 1024
Public Class RequestState
' This class stores the state of the request
Private Shared BUFFER_SIZE As Integer = 1024
Public requestData As StringBuilder
Public bufferRead() As Byte
Public request As WebRequest
Public response As WebResponse
Public responseStream As Stream
Public Sub New()
bufferRead = New Byte(BUFFER_SIZE) {}
requestData = New StringBuilder("")
request = Nothing
responseStream = Nothing
End Sub ' New
End Class ' RequestState
Public Sub GetData(Sender As Object, Args As System.EventArgs)
lblResult.Text += "1"
myWebRequest = WebRequest.Create(dataURL)
Dim myRequestState As New RequestState()
myRequestState.request = myWebRequest
' Start the asynchronous request.
Dim asyncResult As IAsyncResult = CType(myWebRequest.BeginGetResponse(AddressOf RespCallback, myRequestState), IAsyncResult)
lblResult.Text += "2"
allDone.WaitOne()
End Sub
Private Sub RespCallback(asynchronousResult As IAsyncResult)
lblResult.Text += "3"
allDone.Set()
End Sub