简体   繁体   中英

Creating multiple HTTP persistent connections

I know that persistent connection is used by default from HTTP 1.1. What I need to do is to establish more than just one persistent connection, so that I can send requests in multiple persistent connections sessions to one target...

Is it possible to create multiple HTTP persistent connections from one PC to one targeted webserver? For example using HttpWebRequest I have created GET requests to my Apache webserver in VisualStudio (something like code below), but all of them are put together and send by just one persistent connection, according to my Wireshark analysis on webserver.

while(true){
     var request = (HttpWebRequest)WebRequest.Create(URL);
     var request2 = (HttpWebRequest)WebRequest.Create(URL);
     try {
          using (var response = (HttpWebResponse)request.GetResponse()) { 
               Console.WriteLine(response.Headers);
          }
          using (var response2 = (HttpWebResponse)request2.GetResponse()) {
               Console.WriteLine(response2.Headers);
          }
     }
     catch (WebException e) {
          Console.WriteLine(e);
     }
     Thread.Sleep(2000); //wait for sending next request
}

That's done because of the persistent connection purpose, but I want to somehow avoid it and send independent requests by multiple persistent connection sessions. I don't want to do this by using Threads, because that would be too much for CPU and so on. What is the best or other way (using another tools or libraries) how to do this?

You can only send one HTTP Request at a time over a single TCP connection.

HTTP Persistance, (more commonly known as HTTP pipelining) allows you to chain multiple HTTP requests one after the other using the same underlying TCP Connection. This improves performance because the underlying TCP connection that HTTP is running over doesn't need to be reestablished everytime you want to send an HTTP Request.

If you want to run Parallel HTTP Requests, then you need to have one WebRequest() for each parallel request that you want to run. You can then chain multiple requests one after another onto each WebRequest() .

Eg, if you had 100 URL requests that you wanted to perform.

You could create 10 WebRequests, and have each WebRequest sequentially request 10 of 100 URLs.

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