简体   繁体   English

是否可以限制每秒的Web请求数量?

[英]Is it possible to limit the number of web request per second?

Hi i am spidering the site and reading the contents.I want to keep the request rate reasonable. 嗨,我正在抓住网站并阅读内容。我想保持请求率合理。 Up to approx 10 requests per second should probably be ok.Currently it is 5k request per minute and it is causing security issues as this looks to be a bot activity. 每秒最多大约10个请求应该是可以的。目前它是每分钟5k请求并且它导致安全问题,因为这看起来是机器人活动。 How to do this? 这该怎么做? Here is my code 这是我的代码

protected void Iterareitems(List<Item> items)
{
    foreach (var item in items)
    {
        GetImagesfromItem(item);

        if (item.HasChildren)
        {
            Iterareitems(item.Children.ToList());
        }
    }
}

protected void GetImagesfromItem(Item childitems)
{
    var document = new HtmlWeb().Load(completeurl);
    var urls = document.DocumentNode.Descendants("img")
                .Select(e => e.GetAttributeValue("src", null))
                .Where(s => !string.IsNullOrEmpty(s)).ToList();
}

You need System.Threading.Semaphore , using which you can control the max concurrent threads/tasks. 您需要System.Threading.Semaphore ,使用它可以控制最大并发线程/任务。 Here is an example: 这是一个例子:

var maxThreads = 3;
var semaphore = new Semaphore(maxThreads, maxThreads);

for (int i = 0; i < 10; i++)    //10 tasks in total
{
    var j = i;
    Task.Factory.StartNew(() =>
    {
        semaphore.WaitOne();
        Console.WriteLine("start " + j.ToString());
        Thread.Sleep(1000);
        Console.WriteLine("end " + j.ToString());
        semaphore.Release();
    });
}

You can see at most 3 tasks are working, others are pending by semaphore.WaitOne() because the maximum limit reached, and the pending thread will continue if another thread released the semaphore by semaphore.Release() . 您可以看到最多3个任务正在运行,其他正在等待semaphore.WaitOne()因为达到了最大限制,如果另一个线程通过semaphore.Release()释放信号semaphore.Release() ,则挂起的线程将继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM