开发者

VB.NET: How to make a webRequest async

I think this is a simple question for you, but I don't understand other cases of webRequests, so I asked here:

How can I make this webRequest asynchronous?

开发者_JAVA技巧
Dim sBuffer As String
Dim oRequest As WebRequest = WebRequest.Create(url)
oRequest.Method = "GET"
Dim oResponse As WebResponse = oRequest.GetResponse()
Dim oStream As New StreamReader(oResponse.GetResponseStream())
sBuffer = oStream.ReadToEnd()
oStream.Close()
oResponse.Close()
Return sBuffer

Thank you for your help!

Regards, Flo


a function with a simple return value cannot be sinmply made asynchronous, you need to find another method of handling the data that comes back. I would suggest using System.Net.WebClient which is a much easier wrapper on what you've done above, with this class asynch is really easy.

Dim wc As New WebClient
AddHandler wc.DownloadStringCompleted, AddressOf DownloadCompletedHander
wc.DownloadStringAsync(url)

...

Public Shared Sub DownloadCompletedHander(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
    If e.Cancelled = False AndAlso e.Error Is Nothing Then
        Dim myString As String = CStr(e.Result)
        'Do stuff with data
    End If

End Sub 

I don't really speak VB.net but i think thats right from some googling

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜