httpClient请求响应延迟

客户端可以先向服务器端发送一个请求,如果服务器端返回的是状态码100,那么客户端就可以继续把请求体的数据发送给服务器端。这样在某些情况下可以减少网络开销。

再看看HttpClient里面对100-Continue的说明:
The purpose of the Expect: 100-Continue handshake is to allow the client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. The use of the Expect: 100-continue handshake can result in a noticeable performance improvement for entity enclosing requests (such as POST and PUT) that require the target server’s authentication. The Expect: 100-continue handshake should be used with caution, as it may cause problems with HTTP servers and proxies that do not support HTTP/1.1 protocol.

里面提到的一句话是100-Continue应该谨慎使用,因为有些服务器不支持,可能会造成一些问题。那么我这次遇到的原因就是因为服务器端不支持,导致客户端一直要等到100-Continue请求超时以后才把请求体发送给服务器端。
而至于版本的问题是因为HttpClient 3.1和HttpClient 4.1.2都已经把100-Continue默认关闭掉了,只有在HttpClient 4.0.1下才是默认打开的,我奇怪的是HttpClient的文档里面都说了这个应该谨慎使用,为什么还要默认打开?
解决方法
解决方法一:当然,最简单的解决方法当然是更换HttpClient的版本,根据HttpClient的ReleaseNotes,4.1以后的版本都已经将100-Continue默认关闭掉了。
解决方法二:另一个解决方法就是在4.0.1中将100-Continue给关闭掉:
HttpProtocolParams.setUseExpectContinue(params, true);改为HttpProtocolParams.setUseExpectContinue(params, false);
这样就关掉 expectContinue 功能了
或者
1httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

解决方法三:如果你一定想要让100-Continue开着,那么可以减短100-Continue的超时时间,默认的超时时间是最大3秒钟,可以通过以下方法设置:
1httppost.getParams().setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 100);

原文链接:https://blog.csdn.net/qq_25958497/article/details/81985996

猜你喜欢

转载自www.cnblogs.com/muxi0407/p/11589512.html