如何用curl做keepalive http请求?

问题描述:

如何在同一连接中从同一个Web服务器请求多个页面?如何用curl做keepalive http请求?

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

任何人都知道的诀窍?

+0

这不是并行意味着计算。我编辑了问题标题以反映您的实际问题。 – 2011-05-22 07:31:49

+0

@Robin Green所以我猜他*完全想要并发请求 – cnicutar 2011-05-22 07:47:11

+1

如果您使用的是HTTP 1.1协议,除非在请求或响应头中有“Connection:close”,否则keep-alive是默认设置。 – 2013-04-03 04:32:21

我不知道你是否真的意味着“并发”,但从描述我相信你只是想重用连接。如果你只是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 interafce说:

允许在同一个线程多个同时传送,而无需 使它复杂的 应用。

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

+0

不完全。通过保持活着,我的意思是2个请求先发送,那么'curl_easy_perform'被调用(可能两次)来获得响应。 – 2011-05-22 07:40:10

+0

你是如何让卷曲输出这些详细信息的? – 2011-05-22 07:48:46

+0

@ compile-fan'curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);' – cnicutar 2011-05-22 09:04:22