我使用Gradle创建了一个简单的服务器Java应用程序。作为嵌入式服务器,我使用的是Jetty。我也在使用Gretty插件,因为它支持最新的Jetty版本。
项目运行得很好。我试图强调测试它。作为测试的一部分,我需要检查响应时间,因此我通过"Connection:Close"
发送curl
标题。
我的响应是一个很长的JSON字符串,我只看到它的一部分,之后连接挂起。我想知道它为什么会发生,我该如何解决它。
注意:
Connection:Keep-alive
标题时,一切正常如何测试:
./gradlew appRun
curl -H "Connection:Close" -i "http://localhost:8080/Environment/example"
答案 0 :(得分:3)
似乎您在HTTP/1.0
和HTTP/1.1
之间混淆持久连接模式。
要么是这样,要么您使用的是真正旧版本的curl
仍默认为HTTP/1.0
。
HTTP/1.0
有无持久连接,因此要使用持久连接,我们会发送Connection: keep-alive
。
HTTP/1.1
默认使用持久连接,因此要禁用它,我们可以发送Connection: close
使用HTTP/1.0
,Connection: close
就像发送此内容一样......
GET /Environment/example HTTP/1.0
Host: localhost:8080
Connection: close
...根据Connection
规范
HTTP/1.0
生成无效的标头值
让我们使用curl的详细功能来查看真正发生的事情明智的连接......
HTTP/1.1
正常运行:$ curl --verbose --http1.1 http://apache.org/ -so /dev/null
* Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:05:39 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:05:39 GMT
< Content-Type: text/html
<
{ [1125 bytes data]
* Connection #0 to host apache.org left intact
请注意,它表示保持连接完好无损?
HTTP/1.1
手动Connection: close
操作:$ curl --verbose --http1.1 --header "Connection: close" http://apache.org/ -so /dev/null
* Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: close
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:06:35 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:06:35 GMT
< Connection: close
< Content-Type: text/html
<
{ [1106 bytes data]
* Closing connection 0
啊,HTTP响应标头说服务器将关闭,curl看到连接被关闭。我们想要什么。
HTTP/1.0
正常运行:$ curl --verbose --http1.0 http://apache.org/ -so /dev/null
* Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:27 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:27 GMT
< Connection: close
< Content-Type: text/html
<
{ [4002 bytes data]
* Closing connection 0
查看HTTP响应标头如何表示服务器将关闭?
Curl也看到了连接被关闭。
这就是我们对正常HTTP/1.0
操作所期望的。
HTTP/1.0
具有持久连接:$ curl --verbose --http1.0 --header "Connection: keep-alive" http://apache.org/ -so /dev/null
* Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: keep-alive
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:37 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:37 GMT
< Keep-Alive: timeout=30, max=100
< Connection: Keep-Alive
< Content-Type: text/html
<
{ [3964 bytes data]
* Connection #0 to host apache.org left intact
是的,服务器表明它也将使用Keep-Alive(按照HTTP / 1.0规范),并且卷曲甚至同意并说连接保持不变。