cURL在发送HEAD请求时挂起15秒 [英] cURL hanging for 15 seconds when sending HEAD requests

查看:389
本文介绍了cURL在发送HEAD请求时挂起15秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过CLI使用 time 定时了一些HTTP请求, code> wget 和 curl 如下:

I've been timing some HTTP requests via the CLI using time and tools such as wget and curl as follows:

/ usr / bin / time -v wget --spider http:// localhost / index

/ usr / bin / time -v curl http:// localhost / index 2>& 1> / dev / null

我注意到当使用 curl 获得与 wget 相似的响应时间,仅对第一个请求,而对后续请求的响应时间要短得多,如同对 curl 从缓存服务, wget 不是。

What I noticed is that when using curl, I was getting similar response times as with wget only on the first request, and much lower times on subsequent requests, as if the responses to curl were served from cache and wget were not.

调查后,我发现当指定 - spider wget 发出 HEAD 请求可以解释为什么 wget

After investigating I found out that when specifying --spider, wget issues a HEAD request as appended below which could explain why the cache is bypassed with wget:

请求

HEAD /index HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost
Connection: Keep-Alive

HTTP/1.1 200 OK
Date: Mon, 28 Nov 2011 14:45:59 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Location: index.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Set-Cookie: SESS421aa90e079fa326b6494f812ad13e79=16oqmug3loekjlb1tlvmsrtcr2; expires=Wed, 21-Dec-2011 18:19:19 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Mon, 28 Nov 2011 14:45:59 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8


$ b ,发布数据,保存cookie在jar ...)我需要使用 curl 而不是 wget 。因此,我试图用 curl 模拟 HEAD 请求。

Since I'm doing more advanced stuff (writing body and headers in separate files, posting data, saving cookie in jar...) I need to use curl instead of wget. Therefore I'm trying to emulate a HEAD request with curl.

我设法使用 curl 发​​送 HEAD code>如下:

I managed to send a HEAD request with curl as follows:

curlhttp:// localhost / index--requestHEAD-H Keep-Alive-0

请求

HEAD /index HTTP/1.0
User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
Host: localhost
Accept: */*
Connection: Keep-Alive

HTTP/1.1 200 OK
Date: Mon, 28 Nov 2011 15:44:02 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Location: index.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Set-Cookie: SESS421aa90e079fa326b6494f812ad13e79=4001hcmhdbnkb9e2v8nok9lii1; expires=Wed, 21-Dec-2011 19:17:22 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Mon, 28 Nov 2011 15:44:02 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

尽管请求/响应看起来OK,当我执行上述 tcpdump 我可以看到服务器立即响应,但是我的 c $ c> curl > curl 命令总是保持挂起15秒这显然是一个大问题,因为我试图时间我的 curl 命令(FYI之前,我曾经得到一个 curl:(18)传输关闭,剩下的3个字节读取当服务器没有处理 HEAD ,并且正在返回 Content-Length:3 ,但不返回任何内容,但没有一切正常)。

Despite the request/response being seemingly OK, when I execute the above curl command while sniffing with tcpdump I can see that the server responds straight away, however my curl command always stays hanging for exactly 15 seconds which is obviously a big issue since I'm trying to time my curl command (FYI before I used to get a curl: (18) transfer closed with 3 bytes remaining to read when the server was not handling HEAD properly and was returning Content-Length: 3 without returning any content, but no everything looks OK).

我试图使用 - max-time - speed-time code> 200 $ ,但是没有什么区别。

I tried to play with the --max-time and --speed-time arguments to have curl timeout immediately upon receiving the 200 OK but it makes no difference.

问:如何使用curl发送 HEAD 请求,curl命令在收到服务器的响应后立即停止?

Q: How can I send a HEAD request with curl in a way that the curl command stops immediately upon receiving the response from the server?

推荐答案

为什么不使用 -I 选项?

-I/--head
    (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature
    the command HEAD which this uses to get nothing but  the  header
    of  a  document.  When used on a FTP or FILE file, curl displays
    the file size and last modification time only

这篇关于cURL在发送HEAD请求时挂起15秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆