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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM