代理

本文档将介绍代理功能在 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:// URLs 的 HTTP 代理 foopy:80ftp:// URLs 的 HTTP 代理 foopy2:80
  • foopy:80 表示所有 URL 的 HTTP 代理 foopy:80
  • socks4://foopy 表示所有 URL 的 SOCKS v4 代理 foopy:1080

例外的格式可如下所示:

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

    Examples:

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

    Examples:

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

    Examples:

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

    Examples:

      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