我一定是做错了什么,因为这段代码是阻塞的并且是同步运行的,尽管调用async
了GetStringAsync
. 任何帮助都会真正帮助我理解原因:
Private Async Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim urls As List(Of String) = SetUpURLList()
Dim baseAddress = New Uri("http://www.amazon.com")
ServicePointManager.DefaultConnectionLimit = 10
Dim requestNumber As Integer = 0
For Each url In urls
requestNumber += 1
Console.WriteLine("Request #:{0}", requestNumber)
Dim cookies As New CookieContainer()
Dim handler As New HttpClientHandler With {.CookieContainer = cookies, _
.UseCookies = True}
Dim httpClient = New HttpClient(handler) With {.BaseAddress = baseAddress}
Dim response As String = Await HttpClient.GetStringAsync(url).ConfigureAwait(False)
For Each cook As Cookie In cookies.GetCookies(baseAddress)
Console.WriteLine(cook.Name & "=" & cook.Value)
Next
httpClient.Dispose()
Next
Console.WriteLine("Done")
End Sub