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.
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 onConnectionManagerSocketConfig socketConfig=SocketConfig.custom() .setSoTimeout(Timeout.ofMilliseconds(10000)) .build(); BasicHttpClientConnectionManager connMgr=new BasicHttpClientConnectionManager(registry); connMgr.setSocketConfig(socketConfig); CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(connMgr) .build();Setting default host. Followed this How does one set Default HttpHost Target in Apache HttpClient 4.3+? and found that we can override
determineRouteinDefaultRoutePlannerbut we cannot do this in httpclient5 as this method declared asfinalso 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();HttpRequestBasereplaced withHttpUriRequestBase- 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()
- There are no seperate
httpasyncclienthttpcore-niolibraries forHttpClient5- After research, found that everything is bundled inhttpclient5andhttpcore5- 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.