C#每秒限制http请求

时间:2017-01-02 05:27:07

标签: c# http connection httpwebrequest xnet

我有使用代理发送http请求的工具。

我需要每秒限制http请求的帮助。

目前我正在使用以下代码,但我知道这是错误的方法。

所以我的问题是我如何限制每秒最多300个连接和队列线程执行方法限制?

使用xNet:

        static private string generateToken(ProxyType proxyType, string proxy)
    {
        string tokenResponse = null;
        try
        {
            WaitForConnection();

            using (var request = new HttpRequest())
            {
                request.AllowAutoRedirect = true;
                request.ConnectTimeout = 5000;
                request.Reconnect = false;
                request.ReconnectDelay = 5000;
                request.ReconnectLimit = 1;
                request.Proxy = Socks5ProxyClient.Parse(proxy);


                HttpResponse httpWebRequestresponse = request.Get("http://example.com");

                tokenResponse = httpWebRequestresponse.ToString();
            }

        }
        finally
        {
            totalConnections--;
        }
        return tokenResponse;
    }

static void WaitForConnection()
    {
        bool canConnect = false;
        do
        {
            if (totalConnections <= maxconnectionsNum)
            {
                canConnect = true;
            }
        } while (canConnect == false);
        totalConnections++;
    }

0 个答案:

没有答案