Java application sends requests to the Internet and receives responses. I use Apache HTTP Client. Work goes through its HTTP Proxy (3proxy on VPS). Sometimes an error occurs:

сен 24, 2016 5:40:51 PM main_pkg.cycle$JThread1 run SEVERE: null org.apache.http.conn.HttpHostConnectException: Connect to *PROXY_IP*:*PROXY_PORT* [/PROXY_IP*:*PROXY_PORT*] failed: Connection timed out: connect at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:388) 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.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at main_pkg.network.delete_task(network.java:859) at main_pkg.task.task_subscribe(task.java:629) at main_pkg.task.choise_task_type(task.java:506) at main_pkg.cycle$JThread1.run(cycle.java:781) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) ... 11 more 

The request code itself looks like this, the timeout is 61 seconds:

  try{ httpget = new HttpGet("http://myurl.ru/"); httpget.setConfig(config); response = httpclient.execute(httpget, localContext); } catch (ConnectTimeoutException e) { } EntityUtils.consume(response.getEntity()); 

The error occurs as I understand it when the proxy server does not respond. An error occurs on the line = httpclient.execute(httpget, localContext); The problem is not in the server, because this is rarely the case, and any next request passes correctly right away.

How to handle it, so that when it occurs, the same request is sent again, and as a result get the correct answer? You need to use try-catch , but how exactly? What is written now is not working.

    1 answer 1

    Your try-catch catches exceptions of a ConnectTimeoutException type. Guess what happens if there is an exception of another type in this block? That's right, try-catch skip it.

     try{ ... } catch (Exception e) { // TODO } 

    Either you intercept the necessary exceptions one by one and decide what to do with them.

    • Thanks for the reply, so I need to intercept a HttpHostConnectException? - andrshpa
    • one
      @andrshpa, well, even in the stack, ConnectException jumps and you can grab it - ArchDemon