Adding proxy host to APIclient throws an exception

Hi, I have used proxy before and it worked well but normally we do not need it so we have a switch in the application to turn it on. Now I needed the proxy again and it throws an exception though I am confident this worked well in the past.
This is what I do:

  1. Proxy is set: (host and ports are valid, not null)
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

  2. Proxy is added when creating apiclient instance
    apiClient = ApiClient.Builder.standard().withBasePath(cloudRegion).withProxy(proxy).build();

I get the exception at this point:
java.lang.IllegalArgumentException: Inet address may not be null

I tried to trace what the SDK does in ApacheHttpClientConnectorProvider:

  1. grab the proxy options:
    Proxy proxy = properties.getProperty(ApiClientConnectorProperty.PROXY, Proxy.class, null);
  2. grab the address part:
    SocketAddress address = proxy.address();
  3. convert to InetSocketAddress
    InetSocketAddress inetAddress = (InetSocketAddress)address;
  4. create a ProxyHost and set it with the request:
    HttpHost proxyHost = new HttpHost(inetAddress.getAddress(), inetAddress.getPort());
    requestBuilder.setProxy(proxyHost);

I can confirm that this logic is wrong, I was able to reproduce the same isse with a small code without using the SDK!
The problem is that getAddress() method of InetSocketAddress always returns null. This class has a getHostName() and getHostString() and they return the correct host string but the getAddress() does not give back anything. Note that getPort() works well too

here is a simple example:
InetSocketAddress x = new InetSocketAddress("aaa.bbb.com", 8080);
log.info("x getaddress: " + x.getAddress()); // this return null
log.info("x gethostname: " + x.getHostName()); // this returns aaa.bbb.com
log.info("x gethoststring: " + x.getHostString()); // this returns aaa.bbb.com
log.info("x getport: " + x.getPort());

Am I missing something? Not sure how this worked before well but if the SDK uses .getAddress() then the Inet address will be always null and the exception will be raised (if proxy is enabled). I can't be the only one seeing this error when using standard HTTP proxy?

thansk,
Zsolt

First I deleted the post because I just figured out the answer but I post the solution in case someone else runs into it :slight_smile:

The reason getAddress() returned null was that Java wasn't able to resolve the address I had there (it was an internal company address and I was not connected via VPN). While gethostname and gethoststring return the address without any checks, getAddress() gives null if it can't resolve the FQDN provided. So it must be a valid resolvable address or else the APIclient will not be created.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.