If you are using Apache HttpClient , you can create your own DNS resolver to determine the host that you want to redirect, and then specify a replacement IP address.
Note. Simply changing the host header for HTTPS requests does not work. It will throw a "javax.net.ssl.SSLPeerUnverifiedException", forcing you to trust bad certificates, stop SNI, etc., so this is actually not an option. Custom DnsResolver is the only clean way to get these requests to work with HTTPS in Java.
Example:
DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("my.host.com")) { return new InetAddress[] { InetAddress.getByName("127.0.0.1") }; } else { return super.resolve(host); } } }; BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(), null, null, dnsResolver ); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connManager) .build(); HttpGet httpRequest = new HttpGet("https://my.host.com/page?and=stuff"); HttpResponse httpResponse = httpClient.execute(httpRequest);
Johnk source share