20070625

HTML Unit

Link

In a previous post, I mentioned that HTML Unit uses Commons HTTPClient, but wasn't certain if the HTTPClient objects were exposed. As it turns out, the HttpState is exposed - client.webConnection.state will get you there. There are warnings all through the javadoc that this may be gone tomorrow, use at your own risk, but for the time being, that's how you can get through authenticated proxies.

For example:

wc = new WebClient(BrowserVersion.MOZILLA_1_0, '127.0.0.1', 8080)
state = wc.webConnection.state
state.setProxyCredentials(AuthScope.ANY,
new UsernamePasswordCredentials('user','password))
Strangely, there's not a way to set the proxy host and port after the client has been constructed (as far as I can tell), meaning you have to specify a browser version (which is just as well - you're mimicking a browser, why not fake the UserAgent as well?)

0 comments: