获取队列中的http响应并在另一个线程中处理它们

时间:2014-06-07 17:10:50

标签: c# asynchronous httpclient

我构建了一个程序,通过代理每隔5秒向服务器发送一个http请求,以获取信息,如果信息是有趣的,那就是doSomething。 这样的事情:

while(true)
{
    Response response = getResponse(ProxyPool.getProxy(),url).Result;
    if (response != null)
    {
        doSomething(transaction);
    }

    System.Threading.Thread.Sleep(GetRandomNumber(2000, 3001)); //Wait 3~4 sec
}

static async Task<Response> getResponse(CustomProxy proxy, string url)
{
     string json = await RequestHelperAsync.DoProxyRequestAsync(url, proxy);
     // ...
     return response;
}


public static async Task<string> DoProxyRequestAsync(string url, CustomProxy myproxy) 
{
    System.Diagnostics.Stopwatch timer = new Stopwatch();

    var cookies = new CookieContainer();
    var handler = new HttpClientHandler
    {
        CookieContainer = cookies,
        UseCookies = true,
        UseDefaultCredentials = false,
        Proxy = myproxy,
        UseProxy = true,
    };
    HttpClient client = new HttpClient(handler)
    {
        BaseAddress = new Uri(url),
    };
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    string urlContents = null;
    try
    {
        timer.Start();
        Task<string> getStringTask = client.GetStringAsync(url);

        urlContents = await getStringTask;
        timer.Stop();
        myproxy.giveLastReactionTime(timer.Elapsed);
    }
    catch (Exception e)
    {
        // proxyPool.RemoveLastUsed();
        LogHelper.Warning("Fail to connect to proxy " + myproxy.Address + "( " + e + " ), it has been removed from the proxyPool");
        return null;
    }

    return urlContents;
}

我完全同步地使用它,但由于通过代理请求需要花费时间我想每4秒启动一次请求并在队列中获得响应,每次出现时都会在不同的线程中处理。

我已经使我的getResp方法异步,但我现在不知道如何继续......

感谢。

0 个答案:

没有答案