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


JxBrowser provides API that allows configuring proxy settings. By default, JxBrowser uses system proxy settings. Proxy settings are stored in BrowserContext instance. To configure Browser instance with specified proxy settings, you must to initialize Browser instance with BrowserContext configured to use specified proxy configuration.(JxBrowser提供了允许配置代理设置的API。默认情况下,JxBrowser使用系统代理设置。代理设置存储在BrowserContext实例中。要使用指定的代理设置配置Browser实例,必须使用将BrowserContext配置为使用指定的代理配置来初始化Browser实例。)


The following sample demonstrates how to configure Browser instance with DirectProxyConfig:(下面的示例演示如何使用DirectProxyConfig配置浏览器实例:)

String dataDir = FileUtil.createTempDir("chromium-data").getAbsolutePath();

BrowserContextParams contextParams = new BrowserContextParams(dataDir);
contextParams.setProxyConfig(new DirectProxyConfig());

Browser browser = new Browser(new BrowserContext(contextParams));


AutoDetect(自动侦测)

With this proxy configuration the connection automatically detects proxy settings:    (使用此代理配置,连接会自动检测代理设置:)

contextParams.setProxyConfig(new AutoDetectProxyConfig());

Direct(直接)

With this proxy configuration the connection will not use proxy server at all:  (使用此代理配置,连接将完全不使用代理服务器:)

contextParams.setProxyConfig(new DirectProxyConfig());

AutoConfig URL(自动配置网址)

With this proxy configuration the connection uses proxy settings received from proxy auto-config (PAC) file. You must provide a valid URL of the required PAC file: (通过此代理配置,连接将使用从代理自动配置(PAC)文件接收的代理设置。您必须提供所需PAC文件的有效URL:)

contextParams.setProxyConfig(new URLProxyConfig("<pac-file-url>"));

Note: URL to the PAC file must be a valid http:// address. You cannot provide a path to a *.pac file stored on local file system. The name of the PAC file must have the pac extension. For example, http://my-site.com/proxy.pac. On a web server the pac file must be served with the application/x-ns-proxy-autoconfig mime type.(注意:PAC文件的URL必须是有效的http://地址。您无法提供本地文件系统上存储的* .pac文件的路径。 PAC文件的名称必须具有pac扩展名。例如,http://my-site.com/proxy.pac。在Web服务器上,pac文件必须使用application / x-ns-proxy-autoconfig mime类型提供。)


Manual(手册)

With this proxy configuration you can provide custom proxy settings for HTTP, HTTPS and FTP protocols:  (使用此代理配置,您可以为HTTP,HTTPS和FTP协议提供自定义代理设置:)

String proxyRules = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80";
String exceptions = "<local>";  // bypass proxy server for local web pages
contextParams.setProxyConfig(new CustomProxyConfig(proxyRules, exceptions));

 The format of the exceptions string can be any of the following:(异常字符串的格式可以是以下任意一种:)

  • [ URL_SCHEME "://" ] HOSTNAME_PATTERN [ ":" ] e.g. "foobar.com", "*foobar.com", "*.foobar.com", "*foobar.com:99", "https://x.*.y.com:99"([URL_SCHEME“://”] HOSTNAME_PATTERN [“:”]例如“ foobar.com”,“ * foobar.com”,“ * .foobar.com”,“ * foobar.com:99”,“ https:// x。*。y.com:99“)
  • "." HOSTNAME_SUFFIX_PATTERN [ ":" PORT ] e.g. ".google.com", ".com", "http://.google.com"(“。” HOSTNAME_SUFFIX_PATTERN [“:” PORT]例如“ .google.com”,“ .com”,“ http://.google.com”)
  • [ SCHEME "://" ] IP_LITERAL [ ":" PORT ] e.g. "127.0.1", "[0:0::1]", "[::1]", "http://[::1]:99"([方案“://”] IP_LITERAL [“:”端口]例如“ 127.0.1”,“ [0:0 :: 1]”,“ [:: 1]”,“ http:// [:: 1]:99”)
  • IP_LITERAL "/" PREFIX_LENGHT_IN_BITS e.g. "192.168.1.1/16", "fefe:13::abc/33"(IP_LITERAL“ /” PREFIX_LENGHT_IN_BITS例如“ 192.168.1.1/16"、"fefe:13::abc/33”)
  • "<local>" Match local addresses. The meaning of "<local>" is whether the host matches one of: "127.0.0.1", "::1", "localhost".(“ ”匹配本地地址。 “ ”的含义是主机是否匹配以下之一:“ 127.0.0.1”,“ :: 1”,“ localhost”。)

Proxy Authorization(代理授权)

If proxy server requires authorization you can provide login and password programmatically using the following API: (如果代理服务器需要授权,则可以使用以下API以编程方式提供登录名和密码:)

browser.getContext().getNetworkService().setNetworkDelegate(new DefaultNetworkDelegate() {
    @Override
    public boolean onAuthRequired(AuthRequiredParams params) {
        if (params.isProxy()) {
            params.setUsername("proxy-username");
            params.setPassword("proxy-password");
            return false;
        }
        return true;
    }
});

Modifying proxy settings for an already created browser instance(修改已创建的浏览器实例的代理设置)


Since version 6.15, the functionality that allows modifying proxy settings runtime is available in JxBrowser. (从版本6.15开始,JxBrowser中提供了允许修改代理设置运行时的功能。)

You can change proxy settings runtime for specific BrowserContext instance. The proxy configuration will be applied automatically to all Browser instances associated with the BrowserContext.(您可以更改特定的BrowserContext实例的代理设置运行时。代理配置将自动应用于与BrowserContext关联的所有浏览器实例。)


The following sample demonstrates how to use this API:(以下示例演示了如何使用此API:)

...
BrowserContext browserContext = browser.getContext();
ProxyService proxyService = browserContext.getProxyService();
proxyService.setProxyConfig(new CustomProxyConfig("http=foopy:80"));