如何在httpclient 4.3+中更新HttpClient的设置? [英] How to update the settings of an HttpClient in httpclient 4.3+?

查看:171
本文介绍了如何在httpclient 4.3+中更新HttpClient的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在httpclient 4.3中,如果您尊重所有弃用,则必须使用 HttpClientBuilder 构建和配置 HttpClient (文件此处)。方法是明确的,它们似乎易于使用,并且 HttpClient 的界面很清楚。但也许有点太多了。

In httpclient 4.3, if you respect all the deprecations, you must build and configure your HttpClient using an HttpClientBuilder (documentation here). The methods are explicit, they seem easy to use, and HttpClient's interface is clear. But maybe a tad too much.

在我自己的情况下,我必须继承Spring的 HttpComponentsHttpInvokerRequestExecutor (文档这里)。因此,我可以轻松获得 HttpClient ,但我对此对象的了解是它实现了接口。

In my own case, I have to inherit Spring's HttpComponentsHttpInvokerRequestExecutor (documentation here). As a consequence, I can easily get the HttpClient, but all I know about this object is that it implements the interface.

由于客户端已经构建,我对它的实现一无所知,我无法访问 AbstractHttpClient setHttpRequestRetryHandler addRequestInterceptor (虽然是的,我知道,他们弃用d)。

Since the client is already built, and I do not know anything about its implementation, I cannot access methods such as AbstractHttpClient's setHttpRequestRetryHandler or addRequestInterceptor (though yes, I know, they are deprecated).

那么,更新这个 HttpClient 设置的最简洁方法是什么(重试处理程序)请求拦截器是我目前最关心的那些)?我应该...

So, what would be the cleanest way of updating the settings of this HttpClient (the retry handler and request interceptor are those I am most concerned with at the moment)? Should I...


  • ...野蛮地将我的客户端投射到 AbstractHttpClient ,希望我总能收到这个实现吗?

  • ...在我的HttpInvokerRequestExecutor的构造函数中创建一个新的 HttpClient ,得到类似示例的内容转载如下?我可能会补充说Spring的构造函数(至少在3.2.4中)也使用了httpclient 4.3中不推荐使用的方法。使用这种策略会不会有任何副作用?

  • ...做一些我还没有提出的建议?

  • ... savagely cast my client to AbstractHttpClient, hoping I will always receive this implementation?
  • ... create a new HttpClient in my HttpInvokerRequestExecutor's constructor, and get something like the example reproduced below? I might add that Spring's constructor (in 3.2.4 at least) uses methods deprecated in httpclient 4.3 too. Would there be any side effect I missed using this strategy?
  • ... do something I have not proposed yet?

自定义构造函数示例:

public CustomHttpInvokerRequestExecutor() {
    super(); // will create an HttpClient
    // Now overwrite the client the super constructor created
    setHttpClient(HttpClientBuilder.custom(). ... .build());
}


推荐答案


做一些我还没有提出的建议?

do something I have not proposed yet?

我的建议是重新考虑整个方法。不应该在运行时删除/添加协议拦截器,而应该使用 HttpContext 实例来更新请求执行上下文并将配置传递给协议拦截器

My recommendation would be to re-think the entire approach. One should not be removing / adding protocol interceptors at runtime but rather should be making use of HttpContext instance to update request execution context and pass configuration to protocol interceptors

http: //hc.apache.org/httpcomponents-core-4.4.x/tutorial/html/fundamentals.html#d5e306
http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fundamentals.html#d5e223

CloseableHttpClient client = HttpClientBuilder.create()
        .addInterceptorFirst(new HttpRequestInterceptor() {
            @Override
            public void process(
                    final HttpRequest request,
                    final HttpContext context) throws HttpException, IOException {
                boolean b = (Boolean) context.getAttribute("my-config");
                if (b) {
                    // do something useful
                }

            }
        })
        .build();
HttpClientContext context = HttpClientContext.create();
context.setAttribute("my-config", Boolean.TRUE);
CloseableHttpResponse response1 = client.execute(new HttpGet("/"), context);
response1.close();
context.setAttribute("my-config", Boolean.FALSE);
CloseableHttpResponse response2 = client.execute(new HttpGet("/"), context);
response2.close();

这篇关于如何在httpclient 4.3+中更新HttpClient的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆