我使用Gradle创建了一个简单的服务器Java应用程序.作为嵌入式服务器,我使用的是Jetty.我也使用Gretty插件,因为它支持最新的Jetty版本.
该项目运行得很好.我试图强调测试它.作为测试的一部分,我需要检查响应时间,因此我通过curl发送“Connection:Close”标题.
我的响应是一个很长的JSON字符串,我只看到它的一部分,之后连接挂起.我想知道它为什么会发生,我该如何解决它.
注意 :
>发送Connection:Keep-alive标头时,一切都很好
>来自服务器的响应不是长字符串,而是较小.它工作正常(不挂)
>从gradle尝试标准的Jetty插件,结果是一样的.
如何测试:
>从控制台./gradlew appRun构建并运行my project
>从bash控制台运行curl -H“连接:关闭”-i“http:// localhost:8080 / Environment / example”
>看到部分响应和连接还活着……
解决方法:
好像你在混淆HTTP / 1.0和HTTP / 1.1之间的持久连接模式.
要么是这样,要么你使用的是仍然默认为HTTP / 1.0的非常旧的curl版本.
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
…根据HTTP / 1.0规范为Connection生成无效的标头值
让我们使用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:关闭操作:
$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规范),并且卷曲甚至同意并且说连接保持不变.