0

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

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

我会使用BindIPEndPointDelegate(从这里获取)来设置源 IP,然后开始下载页面。

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

4

1 回答 1

0

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

使用配置:

<!-- 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/

于 2018-10-27T22:47:03.193 回答