Yisroel M. Olewski Yisroel M. Olewski - 1 year ago 172
Vb.net Question

HttpWebRequest.BeginGetResponse is not Async

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

Does this look ok?

How come the
line takes about 2-3 seconds to complete before going to
dim a=1

Actually I debugged and I see that the
procedure runs completely 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
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
, but I checked with out the
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

Answer Source


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)
    Dim res = ag.Request.BeginGetResponse(New AsyncCallback(AddressOf FinishResponse), ag)
End Sub

hope this helps someone in the future

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download