This guide shows how to work with the network-related functionality such as proxy, network events, authentication, TLS, client certificate authentication, etc.

Accept Language

JxBrowser allows configuring the Accept-Language HTTP header value via the Network.acceptLanguage(String) method.

For example, the “fr, en-gb;q=0.8, en;q=0.7” value would mean: “I prefer French, but will accept British English, and other types of English”:

network.acceptLanguage("fr, en-gb;q=0.8, en;q=0.7");

Server Whitelist

HTTP Server Authorization Whitelist

You can configure HTTP server authorization whitelist that represents a string with a list of comma/semicolon separated URLs. For example:

network.httpAuthPreferences().serverWhitelist("*google.com,*example.com,*baz");

HTTP Network Delegate Whitelist

To configure HTTP network delegate whitelist you can use the approach described below:

network.httpAuthPreferences().delegateWhitelist("*google.com,*example.com,*baz");

TLS

Certificate Verification

By default Chromium verifies all SSL certificates obtained from a web server during the web page loading. JxBrowser allows modifying this default behavior and take control over the verification process.

To handle certificate verification use the VerifyCertificateCallback callback. Before this callback is invoked, Chromium verifies SSL certificate and provides the results of the verification to the callback. With the results of verification, the callback receives the SSL certificate itself. You can verify the given SSL certificate and notify the engine with the verification results.

For example:

network.set(VerifyCertificateCallback.class, (params) -> {
    // SSL Certificate to verify.
    Certificate certificate = params.certificate();
    // The results of the verification performed by default verifier.
    List<CertVerificationStatus> verificationStatuses =
            params.verificationStatuses();
    // The results of default verification should be used.
    return Response.defaultAction();
});

Client Certificate Authentication

JxBrowser supports authentication using HTTPS client certificates. Please check the Authentication guide for details.

Network Events & Callbacks

The Network API defines a set of events and callbacks that follow the life cycle of a web request. You can use these events to observe and analyze traffic. The callbacks will allow you to intercept, block, or modify requests.

The event life cycle of successful requests looks as follows: Network Events Flow

Before URL Request

The BeforeUrlRequestCallback callback is invoked when an HTTP request is about to occur. You can use this callback to redirect the request to another location. For example:

network.set(BeforeUrlRequestCallback.class, (params) ->
        Response.redirect("<new-url>"));

Before Send Upload Data

The BeforeSendUploadDataCallback callback is invoked before the upload data is sent to a web server. Here you can override the upload data. For example:

network.set(BeforeSendUploadDataCallback.class, (params) ->
        Response.override(TextData.of("<text-data>")));

This callback will not be called if the request does not have the upload data.

The following UploadData types are supported:

  • BytesData represents a sequence of bytes.
  • TextData the data of the text/plain content type.
  • FormData the data of the application/x-www-form-urlencoded content type.
  • MultipartFormData the data of the multipart/form-data content type.

Before Start Transaction

The BeforeStartTransactionCallback callback is invoked before the network transaction starts. In this callback you can add or override the HTTP headers before they are sent out. For example:

network.set(BeforeStartTransactionCallback.class, (params) -> {
    List<HttpHeader> httpHeaders = httpHeaders = new ArrayList<>(params.httpHeaders());
    httpHeaders.add(HttpHeader.of("<header-name>", "<header-value>"));
    return Response.override(httpHeaders);
});

Before Send Headers

The BeforeSendHeadersCallback callback is invoked when a URL request is about to occur and the initial HTTP headers are prepared. In this callback you can add, modify, and delete the HTTP request headers before they are sent to a web server. For example:

network.set(BeforeSendHeadersCallback.class, (params) -> {
    List<HttpHeader> httpHeaders = new ArrayList<>(params.httpHeaders());
    httpHeaders.add(HttpHeader.of("<header-name>","<header-value>"));
    return Response.override(httpHeaders);
});

The following headers are currently not provided to the callback. This list is not guaranteed to be complete nor stable:

- Authorization
- Cache-Control
- Connection
- Content-Length
- Host
- If-Modified-Since
- If-None-Match
- If-Range
- Partial-Data
- Pragma
- Proxy-Authorization
- Proxy-Connection
- Transfer-Encoding

Before Send Proxy Headers

The BeforeSendProxyHeadersCallback callback is invoked when a URL request is about to occur and the initial HTTP headers have been prepared through the proxy connection. In this callback you can add, modify, and delete the HTTP request headers before they are sent to a web server. For example:

network.set(BeforeSendProxyHeadersCallback.class, (params) -> {
    List<HttpHeader> httpHeaders = httpHeaders = new ArrayList<>(params.httpHeaders());
    httpHeaders.add(HttpHeader.of("<header-name>", "<header-value>"));
    return Response.override(httpHeaders);
});

Intercept request

The InterceptRequestCallback callback is invoked when a URL request is about to be sent out. You can use this callback to intercept the request and override the response data as it was sent from a web server. For example:

network.set(InterceptRequestCallback.class, params -> {
    UrlRequestJob urlRequestJob =
            network.newUrlRequestJob(UrlRequestJob.Options
                    .newBuilder(params.urlRequest().id(), HttpStatus.OK)
                    .addHttpHeader(HttpHeader.of("Content-Type", "text/plain"))
                    .addHttpHeader(HttpHeader.of("Content-Type", "charset=utf-8"))
                    .build());
    // Perform complex calculations and override the response data 
    // in the separate thread in order not to freeze the web page.
    new Thread(() -> {
        urlRequestJob.write("My data".getBytes());
        urlRequestJob.complete();
    }).start();
    return InterceptRequestCallback.Response.intercept(urlRequestJob);
});

Transaction Started

The TransactionStarted event is fired when a URL request is about to be sent out. In this event you can access the request and HTTP headers that will be sent out. For example:

network.on(TransactionStarted.class, event -> {
    UrlRequest urlRequest = event.urlRequest();
    List<HttpHeader> httpHeaders = event.httpHeaders();
});

Receive Headers

The ReceiveHeadersCallback callback is invoked for HTTP requests when the headers have been received. Here you can add, modify, or remove the HTTP headers received over the network. For example:

network.set(ReceiveHeadersCallback.class, (params) -> {
    List<HttpHeader> httpHeaders = httpHeaders = new ArrayList<>(params.httpHeaders());
    httpHeaders.add(HttpHeader.of("<header-name>", "<header-value>"));
    return Response.override(httpHeaders);
});

Redirect Response Code Received

The RedirectResponseCodeReceived event is fired when a redirect response code 3xx has been received for the request. In this event you can get the details about the redirect such as new URL and response code. For example:

network.on(RedirectResponseCodeReceived.class, (event) -> {
    String newUrl = event.newUrl();
    int responseCode = event.responseCode();
});

Response Started

The ResponseStarted event is fired when the first byte of the URL response body has been received. For HTTP requests this means that the status line and the response headers are available. In this event you can access the corresponding request and the response code. For example:

network.on(ResponseStarted.class, (event) -> {
    UrlRequest urlRequest = event.urlRequest();
    int responseCode = event.responseCode();
});

Request Completed

The RequestCompleted event is fired when the URL request has been successfully completed or failed. In this event you can check whether the request has been started at all, get the details of the request state, access the response code. For example:

network.on(RequestCompleted.class, (event) -> {
    UrlRequest urlRequest = event.urlRequest();
    // Indicates whether request has been started.
    boolean started = event.isStarted();
    // The details of the URL request state.
    UrlRequestStatus urlRequestStatus = event.status();
    // The HTTP response code.
    int responseCode = event.responseCode();
});

Request Destroyed

The RequestDestroyed event is fired when the request has been destroyed and it cannot be used anymore. To access the details of the destroyed request use the following code:

network.on(RequestDestroyed.class, (event) -> {
    UrlRequest urlRequest = event.urlRequest();
});

Response Bytes Received

The ResponseBytesReceived event is fired when a part of HTTP response body has been received over the network. It allows accessing the bytes of the HTTP response body:

network.on(ResponseBytesReceived.class, event -> {
    byte[] data = event.data();
});
Go Top