如何以编程方式删除 WebClient 中的 2 个连接限制 [英] How can I programmatically remove the 2 connection limit in WebClient

查看:19
本文介绍了如何以编程方式删除 WebClient 中的 2 个连接限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那些精细"的 RFC 要求每个 RFC 客户端注意不要在每个主机上使用超过 2 个连接...

Those "fine" RFCs mandate from every RFC-client that they beware of not using more than 2 connections per host...

微软在 WebClient 中实现了这一点.我知道它可以用

Microsoft implemented this in WebClient. I know that it can be turned off with

App.config:

App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
 <system.net> 
  <connectionManagement> 
   <add address="*" maxconnection="100" /> 
  </connectionManagement> 
 </system.net> 
</configuration> 

(可在 http 上找到://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007 )

但是我怎样才能以编程方式做到这一点?

But how can I do it programmatically?

根据http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

"更改 DefaultConnectionLimit 属性对现有的服务点对象;它只影响服务点对象更改后初始化.如果该财产的价值尚未直接设置或通过配置设置,该值默认为常量 DefaultPersistentConnectionLimit."

"Changing the DefaultConnectionLimit property has no effect on existing ServicePoint objects; it affects only ServicePoint objects that are initialized after the change. If the value of this property has not been set either directly or through configuration, the value defaults to the constant DefaultPersistentConnectionLimit."

我希望在实例化 WebClient 时最好配置限制,但在我的程序开始时以编程方式删除这个可悲的限制也可以.

I'd like best to configure the limit when I instanciate the WebClient, but just removing this sad limitation programmatically at the start of my programm would be fine, too.

我访问的服务器不是 Internet 中的常规 Web 服务器,而是在我的控制下和本地局域网中.我想做 API 调用,但我不使用网络服务或远程处理

The server I access is not a regular webserver in the internet, but under my control and in the local lan. I want to do API-calls, but I don't use webservices or remoting

推荐答案

通过这里和其他地方的一些技巧,我设法通过覆盖我正在使用的 WebClient 类在我的应用程序中解决了这个问题:

With some tips from here and elsewhere I managed to fix this in my application by overriding the WebClient class I was using:

class AwesomeWebClient : WebClient {
    protected override WebRequest GetWebRequest(Uri address) {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);
        req.ServicePoint.ConnectionLimit = 10;
        return (WebRequest)req;
    }
}

这篇关于如何以编程方式删除 WebClient 中的 2 个连接限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆