Using a http proxy server
There is a special WebClient constructor that allows you to specify proxy server information in those cases where you need to connect through one.
@Test public void homePage_proxy() throws Exception { try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX, PROXY_HOST, PROXY_PORT)) { //set proxy username and password final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider(); credentialsProvider.addCredentials("username", "password"); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText()); } }
In case the proxy server requires credentials you can define them on the DefaultCredentialsProvider from the webClient
@Test public void homePage_proxy() throws Exception { try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX, PROXY_HOST, PROXY_PORT)) { //set proxy username and password final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider(); credentialsProvider.addCredentials("username", "password", PROXY_HOST, PROXY_PORT); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText()); } }
Socks proxy sample
The setup of socks proxies is a bit more tricky but in general follows the same pattern.
@Test public void homePage_proxy() throws Exception { try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX) { // socks proxy / the true as last parameter marks this as socks proxy webClient.getOptions().setProxyConfig(new ProxyConfig(SOCKS_PROXY_HOST, SOCKS_PROXY_PORT, null, true)); //set proxy username and password if required final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider(); credentialsProvider.addSocksCredentials("username", "password", SOCKS_PROXY_HOST, SOCKS_PROXY_PORT); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText()); } }