Uri.DnsSafeHost Uri.DnsSafeHost Uri.DnsSafeHost Uri.DnsSafeHost Property

定义

获得可安全用于 DNS 解析的未转义主机名(如必要)。Gets a host name that, after being unescaped if necessary, is safe to use for DNS resolution.

public:
 property System::String ^ DnsSafeHost { System::String ^ get(); };
public string DnsSafeHost { get; }
member this.DnsSafeHost : string
Public ReadOnly Property DnsSafeHost As String

属性值

一个String,其中包含适用于 DNS 解析; 或原始主机字符串格式的 URI 主机部分,如果已适用于解析。A String that contains the host part of the URI in a format suitable for DNS resolution; or the original host string, if it is already suitable for resolution.

异常

此实例代表一个相对 URI,而此属性仅对绝对 URI 有效。This instance represents a relative URI, and this property is valid only for absolute URIs.

示例

下面的示例创建Uri从字符串的实例。The following example creates a Uri instance from a string. 它阐释了如何从返回的值之间的差异Host,其中返回的主机名或地址的 URI 中指定,并且从返回的值DnsSafeHost,这会返回可安全用于 DNS 解析中的地址。It illustrates the difference between the value returned from Host, which returns the host name or address specified in the URI, and the value returned from DnsSafeHost, which returns an address that is safe to use in DNS resolution.

// Create new Uri using a string address.         
Uri^ address = gcnew Uri( "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm" );

// Make the address DNS safe. 
// The following outputs "[fe80::200:39ff:fe36:1a2d]".
Console::WriteLine( address->Host );

// The following outputs "fe80::200:39ff:fe36:1a2d%254".
Console::WriteLine( address->DnsSafeHost );
// Create new Uri using a string address.         
Uri address = new Uri("http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm");

// Make the address DNS safe. 

// The following outputs "[fe80::200:39ff:fe36:1a2d]".
Console.WriteLine(address.Host);

// The following outputs "fe80::200:39ff:fe36:1a2d%254".
Console.WriteLine(address.DnsSafeHost);
    ' Create new Uri using a string address.         
    Dim address As New Uri("http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm")
    
    ' Make the address DNS safe. 
    ' The following outputs "[fe80::200:39ff:fe36:1a2d]".
    Console.WriteLine(address.Host)
    
    ' The following outputs "fe80::200:39ff:fe36:1a2d%254".
    Console.WriteLine(address.DnsSafeHost)

End Sub 'SampleDNSSafeHost

如备注中所述,它在解决前 unescape 的主机名。As explained in Remarks, unescape the host name before resolving it. 可以使用UnescapeDataString方法以取消转义主机名,并且您可以通过调用来解决它GetHostEntry方法。You can use the UnescapeDataString method to unescape the host name, and you can resolve it by calling the GetHostEntry method.

注解

对于 IPv6 地址,删除方括号 ([]) 和ScopeId设置属性,如果构造此实例中时指定了。For IPv6 addresses, the brackets ([]) are removed and the ScopeId property is set, if one was specified when this instance was constructed.

如果已转义的字符串用于构造此实例 (例如,"http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"),然后 DnsSafeHost 返回已转义的字符串。If you used an escaped string to construct this instance (for example, "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"), then DnsSafeHost returns an escaped string. Unescape 从返回任何转义的字符串DnsSafeHost之前使用该字符串进行 DNS 解析 (参阅示例)。Unescape any escaped string returned from DnsSafeHost before using that string for DNS resolution (see the Example). 如果使用无效的非转义的字符串来构造此实例 (例如,"http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm"),然后 DnsSafeHost 返回未转义的字符串。If you used an invalid unescaped string to construct this instance (for example, "http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm"), then DnsSafeHost returns an unescaped string.

DnsSafeHost属性是依赖于配置设置,如本主题后面所述。The DnsSafeHost property is dependent on configuration settings, as discussed later in this topic. Windows 应用商店应用程序,这可能会导致不一致的结果使用时不能更改配置设置DnsSafeHostConfiguration settings cannot be changed by Windows Store applications, which can lead to inconsistent results when using DnsSafeHost. IdnHost属性提供对使用的首选备用DnsSafeHost,因为IdnHost保证始终是安全的无论何种当前 DNS app.config可能设置。The IdnHost property is provided as the preferred alternative to using DnsSafeHost, because IdnHost is guaranteed to always be DNS safe, no matter what the current app.config settings might be.

DnsSafeHost属性已得到扩展,在.NET Framework 3.5 版,3.0 SP1 和 2.0 SP1 提供国际资源标识符 (IRI) 支持根据的 RFC 3987。The DnsSafeHost property has been extended in .NET Framework v3.5, 3.0 SP1, and 2.0 SP1 to provide International Resource Identifier (IRI) support based on RFC 3987. 除非当前用户专门启用 IRI,否则他们看不到任何 NET Framework 2.0 行为的改变。Current users will not see any change from the .NET Framework 2.0 behavior unless they specifically enable IRI. 这确保了 NET Framework 以前版本的应用程序兼容性。This ensures application compatibility with prior versions of the .NET Framework.

若要启用 IRI 支持,以下两项更改是必需的:To enable support for IRI, the following two changes are required:

  1. 添加下面的代码行machine.config .NET Framework 2.0 目录下的文件Add the following line to the machine.config file under the .NET Framework 2.0 directory

    <section name="uri" type="System.Configuration.UriSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /><section name="uri" type="System.Configuration.UriSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  2. 指定是否对域名应用国际化域名 (IDN) 分析以及是否应该应用 IRI 分析规则。Specify whether you want Internationalized Domain Name (IDN) parsing applied to the domain name and whether IRI parsing rules should be applied. 这可以实现machine.config中或在app.config文件。This can be done in the machine.config or in the app.config file. 例如,添加以下代码:For example, add the following:

    <configuration>  
      <uri>  
      <idn enabled="All" />  
      <iriParsing enabled="true" />  
      </uri>  
    </configuration>  
    

启用 IDN 可以将域名中所有 Unicode 标签转换成标签的 Punycode 等同项。Enabling IDN will convert all Unicode labels in a domain name to their Punycode equivalents. Punycode 名称只包含 ASCII 字符,并且始终以 xn-- 前缀开头。Punycode names contain only ASCII characters and always start with the xn-- prefix. 这样是为了支持 Internet 上的 DNS 服务器,因为大部分 DNS 服务器仅支持 ASCII 字符(参见 RFC 3940)。The reason for this is to support existing DNS servers on the Internet, since most DNS servers only support ASCII characters (see RFC 3940).

启用 IDN 只会影响的值DnsSafeHost属性。Enabling IDN only affects the value of the DnsSafeHost property.

有三个可能的 IDN 值具体取决于使用的 DNS 服务器:There are three possible values for IDN depending on the DNS servers that are used:

  • 启用 idn = Allidn enabled = All

    此值会将所有 Unicode 域名转换为它们的 Punycode 等效项(IDN 名称)。This value will convert any Unicode domain names to their Punycode equivalents (IDN names).

  • 启用 idn = AllExceptIntranetidn enabled = AllExceptIntranet

    此值会将所有外部 Unicode 域名转换为使用 Punycode 等效项(IDN 名称)。This value will convert all external Unicode domain names to use the Punycode equivalents (IDN names). 在这种情况下,若要处理本地 Intranet 上的国际化名称,用于 Intranet 的 DNS 服务器应该支持 Unicode 名称。In this case to handle international names on the local Intranet, the DNS servers that are used for the Intranet should support Unicode names.

  • 启用 idn = 无idn enabled = None

    此值不会将任何 Unicode 域名转换为使用 Punycode。This value will not convert any Unicode domain names to use Punycode. 这是默认值是与.NET Framework 2.0 行为一致。This is the default value which is consistent with the .NET Framework 2.0 behaviour.

启用 IRI 分析 (iriParsing 启用 = true) 将执行规范化和字符检查根据最新 IRI 规则在 RFC 3987。Enabling IRI parsing (iriParsing enabled = true) will do normalization and character checking according to the latest IRI rules in RFC 3987. 默认值是false,将执行规范化和字符检查根据 RFC 2396 和 RFC 2732 (适用于 IPv6 文本)。The default value is false and will do normalization and character checking according to RFC 2396 and RFC 2732 (for IPv6 literals).

IRI 支持的详细信息,请参阅备注部分Uri类。For more information on IRI support, see the Remarks section for the Uri class.

适用于