对于获取数据异步,我在公共VB模块中编写了异步HTTPWebrequest函数,然后以三种以上的形式写了一个Get html函数。
在模块中:
Private Sub GetRequestStreamCallback(ByVal asynchronousResult As IAsyncResult)
Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
Dim postStream As Stream = request.EndGetRequestStream(asynchronousResult)
Dim postData As String = <<<<<POST STRING DATA HERE>>>>>
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
postStream.Write(byteArray, 0, postData.Length)
postStream.Close()
Dim result As IAsyncResult = CType(request.BeginGetResponse(AddressOf GetResponseCallback, request), _
IAsyncResult)
End Sub
Private Sub GetResponseCallback(ByVal asynchronousResult As IAsyncResult)
Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
Dim response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), HttpWebResponse)
Dim streamResponse As Stream = response.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim responseString As String = streamRead.ReadToEnd()
<<<<RETURING THE RECEIVED DATA TO THE FORM WHICH CALLED GETTING HTML FUNC.>>>>
streamResponse.Close()
streamRead.Close()
response.Close()
End Sub
Public Sub GetHTMLAsync(ByVal POSTDATA as String, <<<<FUNCTION ADDRESS OR SUCH THING TO CALL WHEN THE ASYNC PROCEDURE IS DONE>>>>)
Req = CType(WebRequest.Create("http://hi.asdf.com/getinformation.php"), HttpWebRequest)
Req.Method = "POST"
Req.ContentType = "application/x-www-form-urlencoded; charset=utf-8"
Req.CookieContainer = CookieC
Req.Timeout = 1000 * 30
<DO SOMETHING TO DELIVER [POSTDATA] AND ["FUNCTION" ARGUMENT] TO ASYNC PROCEDURE ABOVE>
'Async
Dim result As IAsyncResult = CType(Req.BeginGetRequestStream(AddressOf GetRequestStreamCallback, Req), IAsyncResult)
End Sub
形式:
Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
GetHTMLAsync("name=klados&data=birthday", <<<getDataDoneCallBack something..>>>)
End Sub
Public Sub getDataDoneCallBack(ByVal text as String)
'Show the received data(string)
Msgbox(text)
End Sub
因此,当我调用Gethtmlasync时,它将帖子字符串移交给模块中的异步WebRequest函数,当接收到响应数据完成时,它将以表单调用回调函数。
在这里,我只想知道1.如何将帖子字符串移交给getRequestStreamCallback函数?2.如何从模块中的getResponsecallback以特定形式回电?
因为btnget(调用" gethtmlasync"函数)按钮以几种形式存在,而不是单个形式,所以后数据和回电函数不应纠结或跨越。
我真的很感谢答案。祝你有美好的一天!
摘要中的问题
我的问题很简单。使用.NET Framework 3.5(无等待功能),我想制作两个或多个函数(subs),其中包含独立的async httpwebrequests以一种form.vb(或模块,class)。谢谢!
呼叫gethtml(地址,参数,callback_function)
=>获取来自参数的地址的信息
=>如果获得数据,它将调用callback_function
呼叫gethtml可以同时
gethtml(" http://a.com"," name = hi",function1)
gethtml(" http://b.com"," day = happy",function2)
应该单独和独立地工作,不应将接收数据合并。
如果这有些困难,具有相同形式的不同回调功能的不同GetHtml功能也可以!
gethtml_a(" a.com",...)=>回电gothtml_a
gethtml_b(" b.com",...)=>回电gothtml_b
以相同的形式.vb或module.vb也很棒!
这听起来像是单独的背景工作者的好用途。每个背景工作者都使用自己的线程来执行独立于彼此的任务。您可以使用RunWorkerCompleted事件完成后台工作人员后传递数据。
如果使用lambda表达式而不是用于回调的普通函数,那么您的代码将变得更加简单。这样,您将"捕获"您需要传递的状态。这是您的代码外观的简化示例(vb.net对我来说不是一种熟悉的语言,因此,如果有语法问题,则很抱歉):
Public Sub GetHTMLAsync(ByVal postData as String, callback as Action(Of String))
'Req = ...
Req.BeginGetRequestStream( _
Sub (asynchronousResult1)
Dim postStream As Stream = Req.EndGetRequestStream(asynchronousResult1)
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
postStream.Write(byteArray, 0, byteArray.Length)
'...
Req.BeginGetResponse( _
Sub (asynchronousResult2)
Dim response As HttpWebResponse = Req.EndGetResponse(asynchronousResult2)
Dim streamResponse As Stream = response.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim responseString As String = streamRead.ReadToEnd()
callback(responseString)
'...
End Sub, Req)
End Sub, Req)
End Sub
请注意,如何通过像这样定义Sub
内联,我可以访问其范围之外的变量。这称为封闭。因此,BeginGetRequestStream
的回调能够"查看" postData
,BeginGetResponse
的回调能够"查看" callback
,因此可以调用。