代理

本文档将介绍代理功能在 DotNetBrowser 中的工作原理,即如何配置代理设置和处理代理身份验证请求。

DotNetBrowser 默认使用系统代理设置。

系统代理

DotNetBrowser 使用与 Microsoft Internet Explorer 相同的设置。

配置

如果您不想使用系统代理设置,您可以使用 Proxy 为每个 Profile 配置自己的代理设置:

IProxy proxy = profile.Proxy;
Dim proxy As IProxy = profile.Proxy

如果使用 Engine.Proxy() 方法,则会获得与默认配置文件相关联的 Proxy 实例。

如果您想告诉库再次使用系统代理设置,请调用:

engine.Profiles.Default.Proxy.Settings = new SystemProxySettings();
engine.Profiles.Default.Proxy.Settings = New SystemProxySettings()

代理设置存储在用户数据目录中。 如果将引擎配置为使用特定的用户数据目录,那么代理设置将被保存并在下次创建时恢复。

直接

在此配置中,连接完全不使用代理服务器:

engine.Profiles.Default.Proxy.Settings = new DirectProxySettings();
engine.Profiles.Default.Proxy.Settings = New DirectProxySettings()

自动检测

此配置会自动检测代理设置:

engine.Profiles.Default.Proxy.Settings = new AutoDetectProxySettings();
engine.Profiles.Default.Proxy.Settings = New AutoDetectProxySettings()

自定义代理设置

您可以为 HTTP、HTTPS 和 FTP 协议提供自定义代理设置:

string proxyRules = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80";
string exceptions = "<local>";  // 绕过代理服务器访问本地网页。
engine.Profiles.Default.Proxy.Settings = new CustomProxySettings(proxyRules, exceptions);
Dim proxyRules As String = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80"
Dim exceptions As String = "<local>" ' 绕过代理服务器访问本地网页。
engine.Profiles.Default.Proxy.Settings = New CustomProxySettings(proxyRules, exceptions)

代理服务器的 IP 地址在规则中也是可以接受的。 例如,http=127.0.0.1:80

代理规则示例:

  • http=foopy:80;ftp=foopy2 表示对于http:// URL,使用 HTTP 代理foopy:80,对于ftp:// URL,使用 HTTP 代理foopy2:80
  • foopy:80 表示对于所有 URL 使用 HTTP 代理foopy:80
  • socks4://foopy 表示对于所有 URL 使用 SOCKS v4 代理foopy:1080

异常的格式可如下所示:

  • [ URL_SCHEME "://" ] HOSTNAME_PATTERN [ ":" <port> ]

    示例:

    foobar.com
    *foobar.com
    *.foobar.com
    *foobar.com:99
    https://x.*.y.com:99
    
  • "." HOSTNAME_SUFFIX_PATTERN [ ":" PORT ]

    示例:

    .google.com
    .com
    http://.google.com
    
  • [ SCHEME "://" ] IP_LITERAL [ ":" PORT ]

    示例:

    127.0.1
    [0:0::1]
    [::1]
    http://[::1]:99
    
  • IP_LITERAL "/" PREFIX_LENGHT_IN_BITS.

    示例:

    192.168.1.1/16
    fefe:13::abc/33
    
  • "<local>". 匹配本地地址。 <local> 表示主机匹配 127.0.0.1, ::1localhost

如果需要提供多个异常规则,可以用逗号分隔,例如 *foobar.com,.google.com,<local>.

PAC

代理设置是从代理自动配置(PAC)文件中获取的。 您必须提供所需 PAC 文件的有效 URL:

engine.Profiles.Default.Proxy.Settings = new PacProxySettings("<pac-file-url>");
engine.Profiles.Default.Proxy.Settings = New PacProxySettings("<pac-file-url>")

PAC 文件 URL 必须是有效的 http:// 地址。 您不能提供存储在本地文件系统中的 *.pac 文件的路径。 PAC 文件的名称必须具有_pac_扩展名,例如 http://my-site.com/proxy.pac。 Pac 文件必须在网络服务器上以 application/x-ns-proxy-autoconfig mime 类型提供。

有关详细信息,请参阅 PAC 脚本错误部分。

身份验证

DotNetBrowser 支持代理身份验证。 详细内容请参阅身份验证部分。

多个代理

要在同一个 .NET 应用程序中使用多个代理服务器,请为每个代理创建单独的 IEngine 实例。 绑定到同一 IEngine 的所有 IBrowser 实例将共享代理设置。

Go Top