0

I have a small application used to Test connectivity over https. I am using the Apache HttpClient 4.5 to create a HttpClient and invoking a HttpGet call. This works fine for IPv4 but when I try to connect to IPv6 this is failing with the below exception. As far as I know IPv6 must be handeled by Java.

    2023-10-05T12:04:38.906Z java.net.SocketException: Address family not supported by protocol
    2023-10-05T12:04:38.906Z at java.base/sun.nio.ch.Net.connect0(Native Method)
2023-10-05T12:04:38.906Z at java.base/sun.nio.ch.Net.connect(Net.java:579)
2023-10-05T12:04:38.907Z at java.base/sun.nio.ch.Net.connect(Net.java:568)
2023-10-05T12:04:38.907Z at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
2023-10-05T12:04:38.907Z at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
2023-10-05T12:04:38.907Z at java.base/java.net.Socket.connect(Socket.java:633)
2023-10-05T12:04:38.907Z at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:368)
2023-10-05T12:04:38.907Z at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
2023-10-05T12:04:38.907Z at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
2023-10-05T12:04:38.907Z at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
2023-10-05T12:04:38.908Z at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
2023-10-05T12:04:38.908Z at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
2023-10-05T12:04:38.908Z at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
2023-10-05T12:04:38.908Z at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
2023-10-05T12:04:38.908Z at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
2023-10-05T12:04:38.908Z at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
2023-10-05T12:04:38.908Z at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
2023-10-05T12:04:38.908Z at com.ibm.proxy.util.Connection.doTestConnection(Connection.java:123)
The TestConnection method 
`    private boolean doTestConnection(String host, ProtocolPojo protocol) {
        boolean connected = false;
        String serverURL = "https://" + host;
        HttpGet getMethodProxyRequest = new HttpGet(serverURL);
        HttpClientBuilder clientBuilder = HttpClients.custom();

        CloseableHttpClient httpClient = clientBuilder
                .setSSLSocketFactory(protocol.getSecureProtocolSocketFactory())
                .build();
        CloseableHttpResponse proxyResponse = null;
        try {
            RequestConfig reqConf = RequestConfig.custom()
                    .setLocalAddress(InetAddress.getLocalHost())
                    .setAuthenticationEnabled(false).setRedirectsEnabled(false)
                    .setConnectTimeout(10000).setSocketTimeout(15000).build();
            getMethodProxyRequest.setConfig(reqConf);
            proxyResponse = httpClient.execute(getMethodProxyRequest);
        } catch (UnknownHostException e) {
            logException(e);
            logMsgInfo(
                    "Proxy: not able to resolve localhost: " + e.getMessage());
        } catch (IOException e) {
            logException(e);
        } finally {
            if (proxyResponse != null
                    && proxyResponse.getStatusLine() != null) {
                logMsgInfo(
                        "Proxy: " + proxyResponse.getStatusLine().toString());
                logMsgInfo("Proxy: "
                        + proxyResponse.getStatusLine().getStatusCode());
                connected = (proxyResponse.getStatusLine()
                        .getStatusCode() == 200);
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                logException(e);
            }
        }
        return connected;
    }
`

ProtocolPojo class

public class ProtocolPojo {

    private String schema;
    private int port;
    private StrictSSLProtocolSocketFactory secureProtocolSocketFactory;

    public ASMProtocol(String schema,
            StrictSSLProtocolSocketFactory secureProtocolSocketFactory,
            int port) {
        this.schema = schema;
        this.port = port;
        this.secureProtocolSocketFactory = secureProtocolSocketFactory;
    }

    public String getSchema() {
        return schema;
    }

    public int getPort() {
        return port;
    }
    
    /**
     * org.apache.http.conn.ssl.SSLConnectionSocketFactory - that 
     * can be used to verify the host certificate that is 
     * recived.
     */
    public SSLConnectionSocketFactory getSecureProtocolSocketFactory() {
        return secureProtocolSocketFactory.getSSLConnectionSocketFactory();
    }

}

I have checked the IPv6 manually using a web browser, and I see its working fine. I see the issue seems to be inside Apache HttpClient 4.5 is there way to handle this exception. Is this an issue with environment.

2
  • What is ProtocolPojo? That is crucial information that is missing here. Commented Oct 8, 2023 at 22:46
  • Added the ProtocolPojo code above. This is a simple Plain old Java Object that can hold required information for creating an HttpClient object over SSL Commented Oct 9, 2023 at 5:45

1 Answer 1

0

The issue is due to the below block of code.

RequestConfig reqConf = RequestConfig.custom()
.setLocalAddress(InetAddress.getLocalHost())                       
.setAuthenticationEnabled(false).setRedirectsEnabled(false)
.setConnectTimeout(10000).setSocketTimeout(15000).build();

By setting the Local Address to the InetAddress.getLocalHost() which tries to resolve the hostname and gets the resolved address is causing the problem. Removeing that line of code solved the issue. The updated RequestConf is below.

RequestConfig reqConf = RequestConfig.custom()
                 .setAuthenticationEnabled(false).setRedirectsEnabled(false)
                  .setConnectTimeout(10000).setSocketTimeout(15000).build();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.