【问题标题】:How to do keepalive http request with curl?如何使用 curl 进行 keepalive http 请求?
【发布时间】:2011-08-30 12:08:34
【问题描述】:

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

所以客户端需要为每个请求提取响应,当然按照请求的顺序做出响应是服务器的工作。

有人知道诀窍吗?

【问题讨论】:

  • 这不是并发在计算中的含义。我已编辑问题标题以反映您的实际问题。
  • @Robin Green 所以我猜他确实想要并发请求
  • 如果您使用的是 HTTP 1.1 协议,除非请求或响应标头中有“Connection:close”,否则默认设置为 keep-alive。

标签: c http curl keep-alive


【解决方案1】:

我不知道您是否真的指的是“并发”,但从描述中我相信您只是想重用连接。如果你只是 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 文件!”)。

【讨论】:

  • 不完全是。通过保持活动,我的意思是先发送 2 个请求,然后调用 curl_easy_perform(可能两次)以获取响应。
  • @compile-fan curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 2012-03-15
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2014-11-16
相关资源
最近更新 更多