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));


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

contextParams.setProxyConfig(new AutoDetectProxyConfig());


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, 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扩展名。例如,。在Web服务器上,pac文件必须使用application / x-ns-proxy-autoconfig mime类型提供。)


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. "", "*", "*", "*", "https://x.*"([URL_SCHEME“://”] HOSTNAME_PATTERN [“:”]例如“”,“ *”,“ *”,“ *”,“ https:// x。*。“)
  • "." HOSTNAME_SUFFIX_PATTERN [ ":" PORT ] e.g. "", ".com", ""(“。” HOSTNAME_SUFFIX_PATTERN [“:” PORT]例如“”,“ .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. "", "fefe:13::abc/33"(IP_LITERAL“ /” PREFIX_LENGHT_IN_BITS例如“"、"fefe:13::abc/33”)
  • "<local>" Match local addresses. The meaning of "<local>" is whether the host matches one of: "", "::1", "localhost".(“ ”匹配本地地址。 “ ”的含义是主机是否匹配以下之一:“”,“ :: 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() {
    public boolean onAuthRequired(AuthRequiredParams params) {
        if (params.isProxy()) {
            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"));