I made the switch from URLConnection to httpclient 4.5.2 a few days a go, but now I am facing hangs. I am using HTTPClient in a multithreaded application with proxies and here is how my methods look like. After some time the methods get stuck at java.net.SocketInputStream.socketRead and nothing happens. I am using my program in production and I really need to find a fix.
TIMEOUT is set to 5000 and useProxy is set to True public StringBuffer getMeta(int current, int total) throws myappUnauthorizedException { StringBuffer result = new StringBuffer(); String url = "https://api.myapp.com/meta"; CloseableHttpClient httpclient = HttpClientBuilder.create().setMaxConnPerRoute(100000).disableAutomaticRetries().build(); CloseableHttpResponse response = null; URL urlObj; try { urlObj = new URL(url); RequestConfig config = null; if (useProxy) { myapp.Proxy proxyCustom = getRandomProxy(); HttpHost proxy = new HttpHost(proxyCustom.getProxyIp(), proxyCustom.getProxyPort(), "http"); config = RequestConfig.custom().setSocketTimeout(TIMEOUTSOCKET).setConnectTimeout(TIMEOUT).setConnectionRequestTimeout(TIMEOUT).setProxy(proxy).build(); } else if (!useProxy) { config = RequestConfig.custom().setSocketTimeout(TIMEOUTSOCKET).setConnectTimeout(TIMEOUT).setConnectionRequestTimeout(TIMEOUT).build(); } HttpGet request = new HttpGet(url); request.setConfig(config); // add request header request.addHeader("platform", platform); request.addHeader("X-Auth-Token", ThreadLocalManager.myappToken.get()); request.addHeader("User-Agent", USER_AGENT); request.addHeader("os-version", osVersion); request.addHeader("Accept-Language", "en"); request.addHeader("app-version", appVersion); request.addHeader("host", urlObj.getHost()); request.addHeader("Connection", "Keep-Alive"); request.addHeader("Accept-Encoding", "gzip"); response = httpclient.execute(request); int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 403) { throw new myappForbiddenException("403 Status code"); } else if (responseCode == 401) { throw new myappUnauthorizedException("Token not valid!"); } BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } } catch (javax.net.ssl.SSLPeerUnverifiedException | javax.net.ssl.SSLHandshakeException | org.apache.http.NoHttpResponseException | java.net.SocketTimeoutException | org.apache.http.conn.ConnectTimeoutException | org.apache.http.conn.HttpHostConnectException | myappForbiddenException rex) { System.out.println(rex.getMessage()); if (current < total) { AnsiConsole.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Recalling getMeta() " + current).reset()); // System.out.println("Recalling getMeta() " + current); result = getMeta(++current, total); } else { // System.out.println("TOO MANY TRIES ON THE GETMETA METHOD!"); AnsiConsole.out.println(Ansi.ansi().fg(Ansi.Color.RED).bold().a("TOO MANY TRIES ON THE getMeta() METHOD! I GAVE UP!").reset()); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (response != null) response.close(); httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } "Thread4" #13 prio=5 os_prio=0 tid=0x00007fa420386000 nid=0x120a runnable [0x00007fa409efc000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593) at sun.security.ssl.InputRecord.read(InputRecord.java:532) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) - locked <0x0000000706b806b8> (a java.lang.Object) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) - locked <0x0000000706b80678> (a java.lang.Object) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.upgrade(DefaultHttpClientConnectionOperator.java:192) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.upgrade(PoolingHttpClientConnectionManager.java:369) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:415) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) at myapp.Common.getMeta(Common.java:687) at myapp.Common.getMeta(Common.java:714) at myapp.Common.setupNewAccount(Common.java:101) at myapp.myapp.run(Myapp.java:97) at java.lang.Thread.run(Thread.java:745)