Search code examples
javadnshtmlunit

How to override DNS in HTTP connections in Html Unit


I want to override the DNS resolution when using Html Unit, so that I can test an explicit server behind a load balancer. This should be done without changing the etc/hosts file.

This question got's an answer how to solve that task with plain java: How to override DNS in HTTP connections in Java

But I need a solution with Html Unit.

Html Unit uses the apache httpclient. Is it possible to change the connection manager of the httpclient? (see other question above)

final WebClient webClient = new WebClient();
webClient.getOptions().set???

Solution

  • To replace the default HttpClient used by WebClient, call setWebConnection() with a custom subclass of HttpWebConnection where you override createHttpClient().