Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)

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.(JxBrowser提供的功能允许处理网络活动,包括HTTP请求/响应。您可以使用NetworkDelegate处理与指定的BrowserContext关联的Browser实例的所有网络活动。)

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:(使用NetworkDelegate,您可以拦截所有HTTP请求/响应头,并获取有关每个请求/响应阶段的信息。以下是所有请求/响应阶段的列表:)


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.(在即将发生请求时触发。在建立任何TCP连接之前发送此事件,该事件可用于将请求重定向到另一个位置。当方法类型为“ POST”时,可用于访问和修改请求的POST数据。参见示例。)


Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers(当请求即将发生且初始标头已准备好时触发。它允许添加,修改和删除HTTP请求标头)


Fires after onBeforeSendHeaders when proxy connection is used. Provides information about proxy connection, and allows adding, modifying, and deleting HTTP request headers.(使用代理连接时在onBeforeSendHeaders之后触发。提供有关代理连接的信息,并允许添加,修改和删除HTTP请求标头。)


Fires right before the HTTP headers are sent to the network. This event is informational and it does not allow modifying HTTP headers.(在HTTP标头发送到网络之前立即触发。此事件是信息性的,它不允许修改HTTP标头。)


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.(每次收到HTTP(S)响应标头时触发。由于重定向和身份验证请求,每个请求可能会多次发生。此事件旨在允许添加,修改和删除HTTP响应标头,例如传入的Set-Cookie标头。)


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.(当请求收到身份验证质询并且无法使用缓存的凭据进行响应时触发。您可以使用此方法来处理“基本”或“摘要”身份验证。)


Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers.(当请求即将发生且初始标头已准备好时触发。它允许添加,修改和删除HTTP请求标头。)


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.(当接收到响应主体的第一个字节时触发。对于HTTP请求,这意味着状态行和响应头是可用的。此事件是信息性的。)


Fires when a request has been processed successfully or failed.(成功处理请求或失败请求时触发。)


Fires when a request is being destroyed.(销毁请求时触发。)


Fires when engine is about to decide whether specified cookies can be set or not.(在引擎即将决定是否可以设置指定的cookie时触发。)


Fires when engine is about to decide whether specified cookies can be received and send to a web server.(当引擎即将决定是否可以接收指定的Cookie并将其发送到Web服务器时触发。)

The following sample demonstrates how to change target URL using onBeforeURLRequest event and print User-Agent HTTP header value when user loads (以下示例演示了如何在用户加载时使用onBeforeURLRequest事件更改目标URL并打印User-Agent HTTP标头值:)

BrowserContext browserContext = BrowserContext.defaultContext();
browserContext.getNetworkService().setNetworkDelegate(new DefaultNetworkDelegate() {
    public void onBeforeURLRequest(BeforeURLRequestParams params) {
        // If navigate to, then change URL to
        if (params.getURL().equals("")) {

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