[使用 httpcore 4.1.4、httpclient 4.2.5、Oracle JDK 1.7.0_25]
我正在尝试代表 webapp 的 javascript (AJAX) 代码“代理”与第三方 Web 服务的连接,它似乎在大chunked
响应时失败,通过发送多个 RST 并抛出org.apache.http.TruncatedChunkException
.
所以我想知道:
- 为什么http客户端试图断开连接?
- 它在做一些明智的事情吗?(即服务器可能有问题)还是这里发生了什么错误?
我的基本方法是将所有内容从 servlet 的请求对象复制到 apache 组件 httpclient 请求并执行。更具体地说,我:
- 创建一个 apache commons httpclient DefaultHttpClient 对象,
- 将所有请求标头复制到新的请求对象,
host
使用我代理的主机/端口设置(/覆盖)新请求的标头,- 将所有 HTTP 参数复制到新请求中,
- 将任何实体主体复制到新请求中,
- 执行请求,
- 将响应标头复制到我的 servlet 的响应标头,然后
- 将任何实体主体作为流复制到 servlet 的输出流。
引起我问题的一点是最后一个。它似乎在一个块中途失败,我得到以下堆栈跟踪:
org.apache.http.TruncatedChunkException: Truncated chunk ( expected size: 7752; actual size: 4077)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:186)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at <mypackage>.<MyServlet>.service(<MyServlet>.java:XXX)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:63)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)
我已经用 Wireshark 窥探了它并得到了一个类似这样的过程:
source dest info
client server [SYN] seq=0
server client [SYN, ACK] seq=0 ack=1
client server [ACK] seq=1 ack=1
client server GET /url?param=value... HTTP/1.1
server client [ACK] seq=1 ack=221
server client [TCP segment of a reassembled PDU]
client server [ACK] seq=221 ack=4345
client server [FIN, ACK] seq=221 ack=4345
server client [TCP segment of a reassembled PDU]
client server [RST] seq=221
server client Continuation or non-HTTP traffic
client server [RST] seq=221
在我有限的理解中,FIN 的意思是“我已经发送完毕”,因为客户端标头已经发送,所以 IMO 足够公平。然而 RST/reset 似乎只是试图断开连接。
客户端的 HTTP 标头是:
GET /some/path?params=values HTTP/1.1
connection: Keep-Alive
host: target.host.com
accept: */*
user-agent: Wget/1.14 (linux-gnu)
对于服务器:
HTTP/1.1 200 OK
Date: Mon, 16 Sep 2013 03:59:37 GMT
Server: Apache-Coyote/1.1
Content-Disposition: inline; filename=geoserver-GetFeature.text
Content-Type: text/xml; subtype=gml/2.1.2
Vary: Accept-Encoding
Connection: close
Transfer-Encoding: chunked
顺便说一句,这个问题:[restlet ]TruncatedChunkException:看起来很相似,但似乎没有任何有用的信息。
更新:我已经尝试过使用非分块站点(/。:-))并且它同样失败了:
org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body