如何使用curl进行keepalive http请求?

发布于 2024-11-08 18:40:46 字数 101 浏览 0 评论 0原文

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

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

有人知道其中的窍门吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-11-15 18:40:46

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

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 接口表示:

在同一线程中启用多个同时传输,无需
使事情变得复杂
应用程序。

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

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!").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文