如何用curl做keepalive http请求?

时间:2011-05-22 07:21:07

标签: c http curl keep-alive

如何在同一连接中请求来自同一Web服务器的多个页面?

因此客户端需要为每个请求提取响应,当然服务器的工作就是按照请求的顺序进行响应。

有人知道这个伎俩吗?

1 个答案:

答案 0 :(得分:6)

我不知道你是否真的意味着“并发”,但从描述中我相信你只想重用连接。如果只是perform两个对同一服务器的请求,它应该重用连接

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);

以下是输出的部分内容:

* 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)

编辑根据评论

在这种情况下,您需要multi界面。 multi interfce说:

  

在同一个线程中启用多个同时传输而不使用   让它变得复杂   应用

有关示例,请参阅multi-double.c(“只需下载两个HTTP文件!”)。