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

查看:36
本文介绍了如何在 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.

由于客户端已经构建好了,我对它的实现一无所知,所以我无法访问诸如AbstractHttpClientsetHttpRequestRetryHandleraddRequestInterceptor(虽然是的,我知道,它们已被弃用).

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#d5e306http://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天全站免登陆