JxBrowser provides functionality that allows handling network activity including HTTP requests/responses. You can use the NetworkDelegate to handle all network activity of the Browser instances associated with specified BrowserContext.

With NetworkDelegate you can intercept all HTTP requests/responses headers and obtain information about each request/response stage. Below is the list of all request/response stages:


onBeforeRequest

Fires when a request is about to occur. This event is sent before any TCP connection is made and can be used to redirect requests to another location. It can be used to access and modify POST data of the request when method type is "POST". See example.


onBeforeSendHeaders

Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers


onBeforeSendProxyHeaders

Fires after onBeforeSendHeaders when proxy connection is used. Provides information about proxy connection, and allows adding, modifying, and deleting HTTP request headers.


onSendHeaders

Fires right before the HTTP headers are sent to the network. This event is informational and it does not allow modifying HTTP headers.


onHeadersReceived

Fires each time that an HTTP(S) response header is received. Due to redirects and authentication requests this can happen multiple times per request. This event is intended to allow adding, modifying, and deleting HTTP response headers, such as incoming Set-Cookie headers.


onAuthRequired

Fires when a request receives an authentication challenge and is unable to respond using cached credentials. You can use this method to handle "basic" or "digest" authentication.


onBeforeRedirect

Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers.


onResponseStarted

Fires when the first byte of the response body is received. For HTTP requests, this means that the status line and response headers are available. This event is informational.


onCompleted

Fires when a request has been processed successfully or failed.


onDestroyed

Fires when a request is being destroyed.


onCanSetCookies

Fires when engine is about to decide whether specified cookies can be set or not.


onCanGetCookies

Fires when engine is about to decide whether specified cookies can be received and send to a web server.

The following sample demonstrates how to change target URL using onBeforeURLRequest event and print User-Agent HTTP header value when user loads www.google.com: 

BrowserContext browserContext = BrowserContext.defaultContext();
browserContext.getNetworkService().setNetworkDelegate(new DefaultNetworkDelegate() {
    @Override
    public void onBeforeURLRequest(BeforeURLRequestParams params) {
        // If navigate to teamdev.com, then change URL to google.com.
        if (params.getURL().equals("http://www.teamdev.com/")) {
            params.setURL("www.google.com");
        }
    }

    @Override
    public void onBeforeSendHeaders(BeforeSendHeadersParams params) {
        // If navigate to google.com, then print User-Agent header value.
        if (params.getURL().equals("http://www.google.com/")) {
            HttpHeaders headers = params.getHeaders();
            System.out.println("User-Agent: " + headers.getHeader("User-Agent"));
        }
    }
});
Browser browser = new Browser(browserContext);
browser.loadURL("http://www.teamdev.com/");