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 onConnectionManager
SocketConfig 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
determineRoute
inDefaultRoutePlanner
but we cannot do this in httpclient5 as this method declared asfinal
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();
HttpRequestBase
replaced 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
httpasyncclient
httpcore-nio
libraries forHttpClient5
- After research, found that everything is bundled inhttpclient5
andhttpcore5
- 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.
HttpRequester
instead ofHttpClient
? 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