How to update the settings of an HttpClient in httpclient 4.3+?

半城伤御伤魂 提交于 2019-12-21 06:28:40

问题


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.

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.

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).

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...

  • ... 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?

Custom constructor example:

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

回答1:


do something I have not proposed yet?

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();


来源:https://stackoverflow.com/questions/27564351/how-to-update-the-settings-of-an-httpclient-in-httpclient-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!