我正在使用最新的 apache http:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-osgi</artifactId>
<version>4.4.10</version>
</dependency>
我有以下操作:
public void store(InputStream input) throws IOException {
HttpClientBuilder builder = HttpClientBuilder.create();
if (StringUtils.isNotBlank(username)) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username.trim(), StringUtils.trimToEmpty(password));
provider.setCredentials(AuthScope.ANY, credentials);
builder.setDefaultCredentialsProvider(provider);
}
HttpClient client = builder.build();
HttpPost post = new HttpPost(uri);
post.setEntity(new InputStreamEntity(input));
HttpResponse response = client.execute(post);
}
在基本身份验证处于活动状态之前,一切正常,但是,在添加基本身份验证后,我收到以下错误:
引起:org.apache.http.client.NonRepeatableRequestException:无法使用不可重复的请求实体重试请求。在 org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:226) 在 org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) 在 org.apache.http.impl .execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient) .java:185) ... 6 更多
我发现了以下错误报告:https://github.com/http-builder-ng/http-builder-ng/issues/10,但是它被分配给另一个问题。
导致错误的原因是什么?如何在 apache httpclient 中使用基本身份验证?我不知道什么是“可重复的 HTTP 请求”,据我所知,客户端需要设置的只是 Authorization 标头。我是否可能在服务器上错误配置了某些内容,因此它需要“可重复”的 HTTP 请求?
千万里不及你
相关分类