httpclient.GetStringAsync blocking

Kallol

I must be doing something wrong since this code is blocking and runs synchronously, inspite of calling the async method of GetStringAsync. Any help would really help me understand the reasons:

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
i3arnon

Your code isn't blocking, it's just sequential. You are firing each Async operation and asynchronously waiting for it to complete with Await before starting the next one.

If you want to fire all these operations concurrently first create a task for each url and then Await all these tasks at once using Task.WhenAll:

Dim semaphore As New SemaphoreSlim(10)
Async Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
    Dim urls As List(Of String) = SetUpURLList()
    ServicePointManager.DefaultConnectionLimit = 10
    Dim tasks As List(Of Task) = new List(Of Task)()
    For Each url In urls
        tasks.Add(GetUrlAsync(url))
    Next
    Await Task.WhenAll(tasks)
    Console.WriteLine("Done")
End Sub

Async Function GetUrlAsync(url As String) As Task
    Await semaphore.WaitAsync()
    Dim baseAddress = New Uri("http://www.amazon.com")
    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()
    semaphore.Release()
End Sub

*I hope this code makes sense, since I'm not really familiar with VB.Net.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Blocking and Non Blocking subprocess calls

From Dev

Is the ++ operator in System Verilog blocking or non-blocking?

From Dev

Is blocking within a future still blocking?

From Dev

await client.GetStringAsync exits without exception

From Dev

Verilog blocking and non-blocking in a sequence

From Dev

WSARecvFrom blocking

From Dev

Exception handling for httpclient.GetStringAsync(url) async api call

From Dev

Is MFC UpdateAllViews blocking or non blocking?

From Dev

Handling http response codes in GetStringAsync

From Dev

HttpClient.GetStringAsync() call freezes everything without throwing an error, but only under specific conditions

From Dev

Difference between HttpClient.GetStringAsync and WebClient.DownloadStringAsync

From Dev

Deadlock while blocking async HttpClient call within a task continuation

From Dev

Reading the response from HttpClient.GetStringAsync

From Dev

Is setInterval blocking?

From Dev

Blocking sockets

From Dev

How to wrap blocking IO in Scala as non blocking

From Dev

In C#, what is a synchronous alternate for HttpClient.getStringAsync() method?

From Dev

Golang blocking and non blocking

From Dev

c# - GetStringAsync never returns, proper way of doing async GET

From Dev

HttpClient.GetStringAsync(url) throwing "The underlying connection was closed: An unexpected error occurred on a send." Windows 8.1 C#

From Dev

Parser blocking vs render blocking

From Dev

How do I check if GetStringAsync results in a 401 (for instance)?

From Dev

Non Blocking or Blocking assignment for a buffer?

From Dev

await client.GetStringAsync exits without exception

From Dev

WSARecvFrom blocking

From Dev

HttpClient freezes on the GetStringAsync method

From Dev

HttpClient.GetStringAsync() call freezes everything without throwing an error, but only under specific conditions

From Dev

non-blocking and blocking concurrency

From Dev

IHttpFilter implementation causes access violation for HttpClient::GetStringAsync call

Related Related

  1. 1

    Blocking and Non Blocking subprocess calls

  2. 2

    Is the ++ operator in System Verilog blocking or non-blocking?

  3. 3

    Is blocking within a future still blocking?

  4. 4

    await client.GetStringAsync exits without exception

  5. 5

    Verilog blocking and non-blocking in a sequence

  6. 6

    WSARecvFrom blocking

  7. 7

    Exception handling for httpclient.GetStringAsync(url) async api call

  8. 8

    Is MFC UpdateAllViews blocking or non blocking?

  9. 9

    Handling http response codes in GetStringAsync

  10. 10

    HttpClient.GetStringAsync() call freezes everything without throwing an error, but only under specific conditions

  11. 11

    Difference between HttpClient.GetStringAsync and WebClient.DownloadStringAsync

  12. 12

    Deadlock while blocking async HttpClient call within a task continuation

  13. 13

    Reading the response from HttpClient.GetStringAsync

  14. 14

    Is setInterval blocking?

  15. 15

    Blocking sockets

  16. 16

    How to wrap blocking IO in Scala as non blocking

  17. 17

    In C#, what is a synchronous alternate for HttpClient.getStringAsync() method?

  18. 18

    Golang blocking and non blocking

  19. 19

    c# - GetStringAsync never returns, proper way of doing async GET

  20. 20

    HttpClient.GetStringAsync(url) throwing "The underlying connection was closed: An unexpected error occurred on a send." Windows 8.1 C#

  21. 21

    Parser blocking vs render blocking

  22. 22

    How do I check if GetStringAsync results in a 401 (for instance)?

  23. 23

    Non Blocking or Blocking assignment for a buffer?

  24. 24

    await client.GetStringAsync exits without exception

  25. 25

    WSARecvFrom blocking

  26. 26

    HttpClient freezes on the GetStringAsync method

  27. 27

    HttpClient.GetStringAsync() call freezes everything without throwing an error, but only under specific conditions

  28. 28

    non-blocking and blocking concurrency

  29. 29

    IHttpFilter implementation causes access violation for HttpClient::GetStringAsync call

HotTag

Archive