Apache Httpclient在目标正常之前发布数据

时间:2012-10-03 13:18:20

标签: java apache-httpclient-4.x apache-commons-httpclient

有时,httpclient会在服务器接受之前尝试将http发布到服务器。我不知道它发生了哪种情况,但由于这个问题,发布失败了。为什么客户这样做?

以下是日志的一部分;

DEBUG org.apache.http.wire - >> "POST       /destination-path[\r][\n]"
DEBUG org.apache.http.wire - >> "Content-Length: 594[\r][\n]"
DEBUG org.apache.http.wire - >> "Content-Type: application/json[\r][\n]"
DEBUG org.apache.http.wire - >> "Host: url:port[\r][\n]"
DEBUG org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
DEBUG org.apache.http.wire - >> "Expect: 100-continue[\r][\n]"
DEBUG org.apache.http.wire - >> "[\r][\n]"
DEBUG org.apache.http.headers - >> POST /destination-path HTTP/1.1
DEBUG org.apache.http.headers - >> Content-Length: 594
DEBUG org.apache.http.headers - >> Content-Type: application/json
DEBUG org.apache.http.headers - >> Host: url:port
DEBUG org.apache.http.headers - >> Connection: Keep-Alive
DEBUG org.apache.http.headers - >> Expect: 100-continue
DEBUG org.apache.http.wire - >> "data:mydata"

“>>” 中表示此日志中的传出操作。这意味着客户端向服务器发送请求以进行握手。 在最后声明中,正在尝试发送数据。但它应该等待此连接的服务器响应。通常在初始连接时会出现此问题。此尝试失败后连接尝试成功。

1 个答案:

答案 0 :(得分:3)

RFC 2616第8.2.3节:

   Because of the presence of older implementations, the protocol allows
   ambiguous situations in which a client may send "Expect: 100-
   continue" without receiving either a 417 (Expectation Failed) status
   or a 100 (Continue) status. Therefore, when a client sends this
   header field to an origin server (possibly via a proxy) from which it
   has never seen a 100 (Continue) status, the client SHOULD NOT wait
   for an indefinite period before sending the request body.

当使用'expect:continue'握手时,Apache HttpClient在发送请求体之前等待2000毫秒的4xx或100状态。可以使用'http.protocol.wait-for-continue'参数指定不同的值。