简体   繁体   中英

How to force .NET to open many simultaneous http connections

Here is the very simple scenario I would like to show:

internal class Program
{
    private static void Main(string[] args)
    {
        var uri = new Uri("http://myserver.com");
        ServicePointManager.FindServicePoint(uri).ConnectionLimit = 1000;
        for (int i = 0; i < 1000; i++)
        {
            WebRequest.Create(uri).BeginGetResponse(a => { }, null);
        }
        Console.WriteLine("done");
        Console.ReadLine();
    }
}

And corresponding App.config:

<system.net>
  <connectionManagement>
    <clear/>
    <add address="*" maxconnection="1000" />
  </connectionManagement>
  <defaultProxy>
    <proxy proxyaddress="http://127.0.0.1:8888" />
  </defaultProxy>
</system.net>

Let say myserver.com responds 10 seconds (I emulated this in Fiddler via AutoResponder).

In the proxy server (Fiddler) I see that only 14 http requests are sent at application start. Then number of active connections is growing but very slow, about +1 request in 1-2 sec. So after 1 minute of work the number of active http requests is about 50, but not 1000.

Is there any configuration that I can change to force .NET to open if not 1000 real http request but at least 200-300?

I think that the best solution is to open each of the connections in new Thread.

The thread limit is 1023 in .NET 4 32-bit systems 32768 in .NET 4 64-bit systems

If it doesn't work at least you will be sure that there is nothing wrong in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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