【问题标题】:Is ServicePointManager.DefaultConnectionLimit by IP?ServicePointManager.DefaultConnectionLimit 是 IP 吗?
【发布时间】:2017-11-12 17:35:52
【问题描述】:

我正在构建一个网络爬虫,目标站点不允许来自同一 IP 的超过 2 个并发连接。

我的计划是:我会购买一个带有 4 个额外弹性 IP 的 EC2,以更快地获取数据(将是 10 个并发连接,8 个来自其他 IP,2 个来自“主”IP)。

我会使用BindIPEndPointDelegate(取自here)来设置源IP,然后开始下载页面。

所以我的问题来了:DefaultConnectionLimit 将应用于每个源 IP 还是将整个应用程序限制为 2 个并发连接?

【问题讨论】:

  • 从技术上讲,它是 per ServicePoint,实际上应该与 per IP 相同。

标签: c# servicepointmanager


【解决方案1】:

您需要指定主机,否则它适用于所有主机:

使用配置:

<!-- By host -->
<configuration>
 <system.net>
  <connectionManagement>
   <add address="myapi.com" maxconnection="12"/>
   <add address="yourapi.com" maxconnection="8"/>
   <add address="74.125.236.199" maxconnection="4"/>
  </connectionManagement>
 </system.net>
</configuration>

<!-- All hosts -->
<configuration>
 <system.net>
  <connectionManagement>
   <add address="*" maxconnection="15"/>
  </connectionManagement>
 </system.net>
</configuration>

使用代码:

var apiServicePoint1 = ServicePointManager.FindServicePoint("myapi.com");

apiServicePoint1.ConnectionLimit = 12;

var apiServicePoint2 = ServicePointManager.FindServicePoint("yourapi.com");

apiServicePoint2.ConnectionLimit = 8;

参考:https://vincentlauzon.com/2015/12/13/beyond-2-concurrent-connections-in-net/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2012-05-02
    • 2011-08-11
    • 1970-01-01
    • 2011-04-09
    • 2017-09-10
    相关资源
    最近更新 更多