我们有 Spring 的 CommonsHttpInvokerRequestExecutor 的自定义实现。现在,我们希望从httpclient 3.1升级到httpclient 4.3.3,因此我需要实现HttpComponentsHttpInvokerRequestExecutor
。
但是,API 是如此不同,以至于我已经在两点上停留了一段时间(我对 httpclient 3 和 4 都是新手,但我使用 API 文档来解决问题)。
有人知道如何改变这个:
public CustomCommonsHttpInvokerRequestExecutor() {
super();
// No retry.
getHttpClient().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
}
@Override
protected void executePostMethod(final HttpInvokerClientConfiguration config, final HttpClient httpClient, final PostMethod postMethod) throws IOException {
HttpState state = ((CustomHttpInvokerClientConfiguration) config).getState();
if (state.getCredentials(AuthScope.ANY) != null) {
postMethod.setDoAuthentication(true);
httpClient.getParams().setAuthenticationPreemptive(true);
httpClient.getState().setCredentials(AuthScope.ANY, state.getCredentials(AuthScope.ANY));
} else {
httpClient.getParams().setAuthenticationPreemptive(false);
}
httpClient.executeMethod(null, postMethod, state);
}
对此:
public CustomHttpComponentsHttpInvokerRequestExecutor() {
super();
// FIXME default: no retry
// HttpClient client = getHttpClient();
}
@Override
protected HttpResponse executeHttpPost(final HttpInvokerClientConfiguration config, final HttpClient httpClient,
final HttpPost httpPost) throws IOException {
// FIXME Implement
// get credentials with AuthScope.ANY
// if (not null) {
// preemptive authentication
// } else {
// HTTP authentication preemptive is not supported by default
// The else should not be needed
// }
return super.executeHttpPost(config, httpClient, httpPost);
}
- 关于“不重试”问题,我见过的大多数示例都只是假设 HttpClient 的实现并进行野蛮的转换,这是我会尽力避免的。
- 关于“抢占式身份验证”主题,由于 native 不再支持它,因此我搜索并找到了有关如何将其设置为始终处于 Activity 状态的示例,但没有像我自己的情况那样的情况。
非常感谢任何帮助或领导。
最佳答案
因此,在从更全局的角度( How to update the settings of an HttpClient in httpclient 4.3+? )讨论问题之后,这就是我想到的(尚未完全完成,但缺少的拦截器应该不难实现,这要归功于 Preemptive Basic authentication with Apache HttpClient 4 )。
/* Copied from HttpComponentsHttpInvokerRequestExecutor */
private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100;
private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5;
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
public CustomHttpComponentsHttpInvokerRequestExecutor() {
super(makeDefaultHttpClient());
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
}
private static HttpClient makeDefaultHttpClient() {
// New non-deprecated ConnectionManager with same settings as super()
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
// HttpClient with ConnectionManager and no retry
/*
* TODO Add a request interceptor that will authenticate
* if credentials with AuthScope.ANY are provided.
*/
HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
return httpClient;
}
@Override
protected HttpPost createHttpPost(final HttpInvokerClientConfiguration config) throws IOException {
HttpPost httpPost = super.createHttpPost(config);
// Set the timeout for this request if it exists.
Integer timeout = ((CustomHttpInvokerClientConfiguration) config).getReadTimeout();
if (timeout != null) {
RequestConfig rConfig = RequestConfig.copy(httpPost.getConfig()).setSocketTimeout(timeout).build();
httpPost.setConfig(rConfig);
}
return httpPost;
}
一如既往,我会对您可能收到的任何反馈感兴趣。
关于java - 从自定义 Spring 的 CommonsHttpInvokerRequestExecutor 切换到 HttpComponentsHttpInvokerRequestExecutor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317424/