I have some code that iterates a few 100 urls and requests the data from the web.
It looks something like this
for each url in urls
Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
Dim rq = New ReqArgs
rq.Url= url
rq.Request = hwr
Dim res =
hwr.BeginGetResponse(New AsyncCallback(AddressOf FinishWebRequest), rq)
Dim a = 1
next
Does this look ok?
How come the BeginGetresponse
line takes about 2-3 seconds to complete before going to dim a=1
?.
Actually I debugged and I see that the FinishWebRequest
procedure runs c开发者_Go百科ompletely before the Dim a=1
is reached.
So is this async?
I'm not earning any time by using the async. am I? Or is there a different way to do this?
The point is that the main sub should fire off 300 requests and return control to the UI, then the FinishWebRequest
should process them slowly on its own thread and own time , as the requests come in.
How do I do that?
Btw, the main sub is running in a BackgroundWorker
, but I checked with out the BackgroundWorker
and the problem is the same
It seems that the answer should be here but its just not working for me
I'm WPF 4.0
Appreciate your help and advice. Thanks
yup
the problem was with the POST
i now start the post writing like this
Dim ReqStream = hwr.BeginGetRequestStream(New AsyncCallback(AddressOf FinishRequestStream), rq)
and then my callback is like this
Sub FinishRequestStream(ByVal result As IAsyncResult)
Dim ag = CType(result.AsyncState, ReqArgs)
Dim postStream = ag.Request.EndGetRequestStream(result)
Dim PostBytes = Encoding.UTF8.GetBytes(ag.PostText)
postStream.Write(PostBytes, 0, PostBytes.Length)
postStream.Close()
Dim res = ag.Request.BeginGetResponse(New AsyncCallback(AddressOf FinishResponse), ag)
End Sub
hope this helps someone in the future
Reposting this from another question.
From the documentation on HttpWebRequest.BeginGetResponse Method:
The BeginGetResponse method requires some synchronous setup tasks to complete (DNS resolution, proxy detection, and TCP socket connection, for example) before this method becomes asynchronous. [...] it might take considerable time (up to several minutes depending on network settings) to complete the initial synchronous setup tasks before an exception for an error is thrown or the method succeeds.
To avoid waiting for the setup, you can use HttpWebRequest.BeginGetRequestStream Method but be aware that:
Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the BeginGetRequestStream method, you must use the BeginGetResponse method to retrieve the response.
精彩评论