1

我确实有以下情况:

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 Client API)时,存储响应 407 Authentication Required。Apache Http 客户端进行初始握手,然后重新发送请求,但现在没有实体,因为它已被第一个请求消耗。

一种解决方案是从客户端缓存实体,但它可能非常大,超过 1 GB。

问题是否有更好的解决方案,例如仅预发送请求的标头?

4

1 回答 1

0

使用expect-continue握手。

CloseableHttpClient client = HttpClients.custom()
        .setDefaultRequestConfig(
                RequestConfig.custom()
                        .setExpectContinueEnabled(true)
                        .build())
        .build();
于 2020-04-04T10:22:44.587 回答