Some domain names are resolved to several IPs by DNS. I need to make http requests to all those IPs (a kind of service healthcheck). I can use
InetAddress[] addrs = InetAddress.getAllByName("google.com")
to resolve all IPs which should be checked. It is not about google, but it is with testing on google that I've found puzzles
I'm trying to use Apache HttpClient to make request. Like this:
HttpHead req = new HttpHead(url);
//InetAddress addr = InetAddress.getByAddress("google.com", new byte[] {74, 125, (byte) 131, 101});
InetAddress addr = // one of those we got from InetAddress.getAll...
CloseableHttpResponse resp = cli.execute(new HttpHost(addr, 443, "https"), req);
It seems to work, however I decided to check it constructing the IP manually - see the commented string. If it is uncommented and used instead
of the addr from the dns enumeration, all works fine, returning 200.
But if I then change IP address to 8, 8, 8, 8 in this line and expect
to get connection timeout (as it is address of google's dns which don't
listen on 443 port, it is firewalled) - I see 404 instead. Thus I am not sure it uses the specified IP for real connection rather than for verification.
This makes me think that the approach is wrong and I use this HttpHost incorrectly.
Alternative approach is to use custom DnsResolver set up for given
HttpClient, which will feed only specific IPs, and do it in turns. however this looks like ugly and inconvenient hack so I'm searching for "proper" way...
P.S. variant with sending requests in plain text to sockets (without any library) will work fine, but most probably solution will need to work with https also
which makes this variant difficult.