Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do keepalive http request with curl?

How can I request multiple pages from the same web server within the same connection?

So the client side need to extract the response for each request,of course it's the server's job to make the response in the same order as requested.

Anyone knows the trick?

like image 959
compile-fan Avatar asked May 22 '11 07:05

compile-fan


People also ask

How do I keep my HTTP request alive?

Keep-Alive is enabled by explicitly requesting it via HTTP header. If you don't have access to your web server configuration file, you can add HTTP headers yourself by using . htaccess file.

How do I reuse a curl connection?

When you are using the easy API, or, more specifically, curl_easy_perform() , libcurl will keep the pool associated with the specific easy handle. Then reusing the same easy handle will ensure it can reuse its connection.

What does HTTP keepalive do?

The HTTP keep-alive header maintains a connection between a client and your server, reducing the time needed to serve files. A persistent connection also reduces the number of TCP and SSL/TLS connection requests, leading to a drop in round trip time (RTT).

Is http keep alive default?

Keep-alive connections are enabled by default in HTTP/1.1 while not in HTTP/1.0. HTTP/1.0 was designed to close the connection after every request between client and server. We can actually check this difference using telnet .


1 Answers

I don't know if you really meant "concurrent", but from the description I believe you just want to reuse the connection. If you simply perform two requests to the same server, it should reuse the connection

persistant.c

/* get the first document */ 
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
res = curl_easy_perform(curl);


/* get another document from the same server using the same
   connection */ 
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/docs/");
res = curl_easy_perform(curl);

Here are portions of the output:

* About to connect() to example.com port 80 (#0)
*   Trying 192.0.32.10... * connected
* Connected to example.com (192.0.32.10) port 80 (#0)

[...]

* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
Connection: Keep-Alive

[...]

* Connection #0 to host example.com left intact
* Re-using existing connection! (#0) with host example.com
* Connected to example.com (192.0.32.10) port 80 (#0)

EDIT In light of comment

In that case you need the multi interface. The multi interafce says:

Enable multiple simultaneous transfers in the same thread without making it complicated for the application.

For an example, see multi-double.c ("Simply download two HTTP files!").

like image 126
cnicutar Avatar answered Sep 22 '22 13:09

cnicutar