HttpClient5 - Lot of APIs changed / removed
H

0

13

I'm migrating my application to use HttpClient5 but its becoming miserable day by day. Many APIs removed and no proper documentation available to know alternatives. Stackoverflow/any other website/blogs always shows about answers related to httpcomponents4.x but those APIs no longer exist in HttpClient5. I'm putting all my queries / alternatives here. If anyone knows, please answer / confirm if those are correct implementations.

  1. Setting socket timeout on client: Removed RequestConfig.custom().setSocketTimeout(socketTimeout).build() API. After lot of research, found that there is seperate SocketConfig class which need to set on ConnectionManager

     SocketConfig socketConfig=SocketConfig.custom()
                                     .setSoTimeout(Timeout.ofMilliseconds(10000))
                                     .build();
    
     BasicHttpClientConnectionManager connMgr=new BasicHttpClientConnectionManager(registry);
     connMgr.setSocketConfig(socketConfig);
    
     CloseableHttpClient httpclient = HttpClients.custom()
             .setConnectionManager(connMgr)
             .build();
    
  2. Setting default host. Followed this How does one set Default HttpHost Target in Apache HttpClient 4.3+? and found that we can override determineRoute in DefaultRoutePlanner but we cannot do this in httpclient5 as this method declared as final so cannot override. So i did this:

         HttpHost targetHost = new HttpHost(myHost,myPort);
         HttpRoutePlanner planner=new HttpRoutePlanner() {
    
             @Override
             public HttpRoute determineRoute(HttpHost var1, HttpContext var2) throws HttpException {
                 HttpRoute route=new HttpRoute(targetHost);//default for all requests
                 return route;
             }
         };
            CloseableHttpClient client=HttpClients.custom()
                 .setConnectionManager(mgr)
                 .setRoutePlanner(planner)
                 .build();
    
  3. HttpRequestBase replaced with HttpUriRequestBase - on HttpRequestBase we were calling getRequestLine() to know the request details which is printing like below:

POST https://url HTTPS/1.1

But getRequestLine() API removed in HttpUriRequestBase, so i'm using like this:

httpReq.getMethod()+" "+httpReq.getUri() but its not printing protocol even if i use httpReq.getVersion()

  1. There are no seperate httpasyncclient httpcore-nio libraries for HttpClient5 - After research, found that everything is bundled in httpclient5 and httpcore5 - Now more struggle starts here. All below classes no longer exist.
SSLIOSessionStrategy 
NoopIOSessionStrategy
PoolingNHttpClientConnectionManager
DefaultConnectingIOReactor
ConnectingIOReactor
IOReactorException

Dont know the alternatives for this yet. IOReactorConfig apis are not clear. In the demo examples, IOReactorConfig was set on CloseableHttpAsyncClient without SSLcontext examples. In my case as per existing code, they are using IOReactorConfig and SSLContext . Now not sure how to club these.

Please suggest if i'm doing correct or not.

Lot of others are there. Will update.

Hummock answered 23/6, 2021 at 9:17 Comment(5)
I recently switched my work automation tests project from Apache Client 4.5.10 to 5.2.1, and I was able to set literally all the configuration from old client in the new one, but yeah, it was pain in the a*sHerriot
the documentations are really poor.Patten
There is zero accessible documentation about the rationale behind these extensive changes. I'm not seeing any benefits, yet. Just looking at them now since we are just now adopting Spring Boot 3 and I'm trying to fix all the deprecation warnings. Why HttpRequester instead of HttpClient? Also, I'm seeing a lot of redundant information passed into the execution API. Easy to get conflicting information that way. I presume changes are to better support async cases and/or HTTP2, WebSockets, etc? But I'm not seeing the connection, yet.Stempson
@CharlieReitzel did you end up finding that connection?Madelene
@MissSkooter No, there haven't been any updates to the documentation. It's not difficult to make the updates, just tedious with no apparent benefit. :--)Stempson

© 2022 - 2024 — McMap. All rights reserved.