本文介绍了httpclient.GetStringAsync堵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一些错误的,因为这code调用的异步 GetStringAsync 。任何帮助将真正帮助我理解的原因:

 专用异步子btnTest_Click(发送者为对象,E作为EventArgs的)把手btnTest.Click
    昏暗的URL方式列表(串)= SetUpURLList()
    昏暗baseAddress =新的URI(http://www.amazon.com)
    ServicePointManager.DefaultConnectionLimit = 10
    昏暗requestNumber为整数= 0
    每个网址URL中
        requestNumber + = 1
        Console.WriteLine(请求#:{0},requestNumber)
        昏暗饼干作为新的CookieContainer()
        昏暗的处理程序作为新HttpClientHandler随着{.CookieContainer =饼干,_
                                                   .UseCookies = TRUE}
        昏暗的HttpClient =新的HttpClient(处理器)有{.BaseAddress = baseAddress}
        昏暗的响应作为字符串=等待HttpClient.GetStringAsync(URL).ConfigureAwait(假)
        对于每一个厨师作为曲奇在cookies.GetCookies(baseAddress)
            Console.WriteLine(cook.Name&放大器;=与& cook.Value)
        下一个
        httpClient.Dispose()
    下一个    Console.WriteLine(完成)
结束小组


解决方案

您code没有阻止,只是顺序。你是射击每个异步运行和异步等待它与等待完成启动下一个了。

如果你想同时发射所有这些操作首先为每个网​​址然后等待所有这些任务一次使用 Task.WhenAll 任务:

 昏暗的信号作为新SemaphoreSlim(10)
异步子btnTest_Click(发送者为对象,E作为EventArgs的)把手btnTest.Click
    昏暗的URL方式列表(串)= SetUpURLList()
    ServicePointManager.DefaultConnectionLimit = 10
    昏暗的任务列表(任务)=新名单(任务)()
    每个网址URL中
        tasks.Add(GetUrlAsync(URL))
    下一个
    等待Task.WhenAll(任务)
    Console.WriteLine(完成)
结束小组异步功能GetUrlAsync(URL作为字符串)作为任务
    等待semaphore.WaitAsync()
    昏暗baseAddress =新的URI(http://www.amazon.com)
    昏暗饼干作为新的CookieContainer()
    昏暗的处理程序作为新HttpClientHandler随着{.CookieContainer =饼干,_
                                               .UseCookies = TRUE}
    昏暗的HttpClient =新的HttpClient(处理器)有{.BaseAddress = baseAddress}
    昏暗的响应作为字符串=等待HttpClient.GetStringAsync(URL).ConfigureAwait(假)
    对于每一个厨师作为曲奇在cookies.GetCookies(baseAddress)
        Console.WriteLine(cook.Name&放大器;=与& cook.Value)
    下一个
    httpClient.Dispose()
    semaphore.Release()
结束小组

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
解决方案

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

这篇关于httpclient.GetStringAsync堵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:17