GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.htmlunit.org — WebClient - the browser – HtmlUnit
IntroductionThe WebClient represents the browser when you work with HtmlUnit. To start using HtmlUnit you have to instantiate a new WebClient—like starting a browser in the real world. WebClient implements AutoCloseable; you should always use it with try-with-resources constructs. After a WebClient is closed (see WebClient.close()), any further use is not supported and might lead to exceptions or incorrect behavior.
Imitating a specific browserOften you will want to simulate a specific browser. This is done by passing a org.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers.
Specifying this BrowserVersion will change:
to match real browsers. In most cases, it should be sufficient to use the predefined BrowserVersion constants. Using the options to adjust the browserThere are various options available to make fine-grained adjustments to the browser.
The default values for most options are similar to the default values of real browsers, but (as always) there is one important exception: HtmlUnit stops JavaScript execution at the first unhandled exception—browsers do not stop. You can change this by changing the throwExceptionOnScriptError option to false.
For a complete list and more details please have a look at the WebClientOptions API. Change the browser language / time zoneChanging the language/time zone cannot be done from the options; it must be done before the WebClient is created. All BrowserVersions are shipped with 'en-US' as the language and 'America/New_York' as the timezone. To change these default settings, a customized copy of the corresponding BrowserVersion must be created using the BrowserVersionBuilder. This new BrowserVersion can then be used to create a WebClient.
There is no support for changing the language/timezone after the WebClient has been created. For more details please have a look at the BrowserVersion.BrowserVersionBuilder API. Change the browser user agentChanging the user agent is similar to changing language/time zone (see above). You have to create a customized copy of the corresponding BrowserVersion using the BrowserVersionBuilder. This adapted BrowserVersion can then be used to create a WebClient.
For more details please have a look at the BrowserVersion.BrowserVersionBuilder API. Using HtmlUnit behind a proxyUsing a http proxyThere is a special WebClient constructor that allows you to specify proxy server information in those cases where you need to connect through one.
In case the proxy server requires credentials you can define them on the DefaultCredentialsProvider from the webClient.
Socks proxy sampleThe setup of SOCKS proxies is a bit more tricky but in general follows the same pattern.
WebWindowListener / WebWindowEventsIf you wish to be notified when windows are created or pages are loaded, you need to register a WebWindowListener with the WebClient using WebClient.addWebWindowListener(WebWindowListener). When a window is opened either by JavaScript or through the WebClient, a WebWindowEvent will be fired and passed into the WebWindowListener.webWindowOpened(WebWindowEvent) method. Note that both the new and old pages in the event will be null as the window does not have any content loaded at this point. If a URL was specified during creation of the window then the page will be loaded and another event will be fired as described below. When a new page is loaded into a specific window, a WebWindowEvent will be fired and passed into the WebWindowListener.webWindowContentChanged(WebWindowEvent) method. Using handlersThere are many handlers used by the WebClient for special purposes. These handlers implement specific interfaces, and you are able to replace them with your own implementations. Default implementations are also available. AlertHandler
The handler to be used to process JavaScript alerts triggered when the JavaScript method ConfirmHandler
The handler for the JavaScript function PromptHandler
The handler for the JavaScript function StatusHandler
A handler for changes to AttachmentHandler
A handler for attachments, which represent pages received from the server containing
ClipboardHandlerA handler for clipboard access. PrintHandler
A handler for providing WebStartHandlerA handler for WebStart support. FrameContentHandlerA handler to make a decision to load the frame content or not. See Blocking loading of frame content for a sample. CSSErrorHandlerA handler for CSS parser error processing. OnbeforeunloadHandler
A handler for JavaScript RefreshHandlerA handler for page refreshes. PolyfillsThe number of JavaScript APIs supported by browsers seems to increase every day. Because of the limited development resources of the HtmlUnit project, keeping track with all of them is challenging. However, there are many polyfills available to add API support for older browsers. The idea is to use some of these polyfills to add missing APIs. Starting with version 2.59.0, HtmlUnit supports the integration of polyfills. There is a dedicated option for every supported polyfill (disabled by default), and when enabled, the polyfill is automatically loaded.
Fetch API Polyfill
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |