S
S
Sergey Tryapkin2019-03-06 14:37:42
Java
Sergey Tryapkin, 2019-03-06 14:37:42

Checking website availability via Apache HttpClient 4?

Good afternoon.
Help me figure out what my problem is.
There is this method:

private static Boolean checkHttp() throws IOException {
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(80000)
                .setConnectionRequestTimeout(80000)
                .setSocketTimeout(80000).build();

        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        HttpResponse response = client.execute(new HttpGet(TestHttpStatus.HTTP_URL));
        int statusCode = response.getStatusLine().getStatusCode();
        client.close();
        return statusCode == 200;
    }

This method is run multiple times after the service is restarted on the remote server.
The problem is that the first time it works fine, but when I try to call it further in the code, I get an exception:
org.apache.http.conn.HttpHostConnectException: Connect to 1.2.0.3:8023 [/1.2.0.3] failed: Connection refused: connect
  at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
  at TestHttpStatus.checkHttp(TestHttpStatus.java:46)
  at TestHttpStatus.main(TestHttpStatus.java:32)
Caused by: java.net.ConnectException: Connection refused: 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:75)
  at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
  ... 11 more

It seems that I use CloseableHttpClient and close it, but it still doesn't work.
I'm looking for help or advice on how to fix this.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question