![](/img/trans.png)
[英]Unexpected error occurred in http client. org.apache.commons.httpclient.ProtocolException: Unbuffered entity enclosing request can not be repeated
[英]Apache Http Client execute request without sending the enclosing entity
我確實有以下情況:
1) 客戶端通過套接字向服務器發送帶有封閉實體的 HTTP 請求。
2)服務器將封閉實體上傳到另一個位置,我們稱之為存儲。
我只需要實現服務器。
到目前為止,我能夠使用Apache HTTP 組件庫來實現它,使用類似的東西:
// The request from the client
org.apache.http.HttpRequest request = ...;
// The org.apache.http.entity.InputStreamEntity will
// read bytes from the socket and write to the Storage
HttpEntity entity = new InputStreamEntity(...)
BasicHttpEntityEnclosingRequest requestToStorage = new ......
requestToStorage.setEntity(entity);
CloseableHttpClient httpClient = ...
CloseableHttpResponse response = httpClient.execute(target, requestToStorage );
到目前為止,一切都很好。 問題是,存儲服務器需要身份驗證。 當服務器發出第一個請求時(通過 Apache Http 客戶端 API),存儲響應 407 Authentication Required。 Apache Http 客戶端進行初始握手然后重新發送請求,但現在沒有實體,因為它已經被第一個請求消耗。
一種解決方案是從客戶端緩存實體,但它可能非常大,超過 1 GB。
問題是否有更好的解決方案,例如僅預發送請求的標頭?
使用expect-continue
握手。
CloseableHttpClient client = HttpClients.custom()
.setDefaultRequestConfig(
RequestConfig.custom()
.setExpectContinueEnabled(true)
.build())
.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.