通过


IdnMapping 类

定义

支持对 Internet 域名使用非 ASCII 字符。 无法继承此类。

public ref class IdnMapping sealed
public sealed class IdnMapping
type IdnMapping = class
Public NotInheritable Class IdnMapping
继承
IdnMapping

示例

以下示例使用 GetAscii(String, Int32, Int32) 此方法将国际化域名数组转换为 Punycode。 然后,该方法 GetUnicode 将 Punycode 域名转换回原始域名,但将原始标签分隔符替换为标准标签分隔符。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] names = { "bücher.com", "мойдомен.рф", "παράδειγμα.δοκιμή",
                         "mycharity\u3002org",
                         "prose\u0000ware.com", "proseware..com", "a.org",
                         "my_company.com" };
      IdnMapping idn = new IdnMapping();

      foreach (var name in names) {
         try {
            string punyCode = idn.GetAscii(name);
            string name2 = idn.GetUnicode(punyCode);
            Console.WriteLine("{0} --> {1} --> {2}", name, punyCode, name2);
            Console.WriteLine("Original: {0}", ShowCodePoints(name));
            Console.WriteLine("Restored: {0}", ShowCodePoints(name2));
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a valid domain name.", name);
         }
         Console.WriteLine();
      }
   }

   private static string ShowCodePoints(string str1)
   {
      string output = "";
      foreach (var ch in str1)
         output += $"U+{(ushort)ch:X4} ";

      return output;
   }
}
// The example displays the following output:
//    bücher.com --> xn--bcher-kva.com --> bücher.com
//    Original: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
//    Restored: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
//
//    мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф
//    Original: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
//    Restored: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
//
//    παράδειγμα.δοκιμή --> xn--hxajbheg2az3al.xn--jxalpdlp --> παράδειγμα.δοκιμή
//    Original: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
//    Restored: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
//
//    mycharity。org --> mycharity.org --> mycharity.org
//    Original: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+3002 U+006F U+0072 U+0067
//    Restored: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+002E U+006F U+0072 U+0067
//
//    prose ware.com is not a valid domain name.
//
//    proseware..com is not a valid domain name.
//
//    a.org --> a.org --> a.org
//    Original: U+0061 U+002E U+006F U+0072 U+0067
//    Restored: U+0061 U+002E U+006F U+0072 U+0067
//
//    my_company.com --> my_company.com --> my_company.com
//    Original: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
//    Restored: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim names() As String = { "bücher.com", "мойдомен.рф", "παράδειγμα.δοκιμή",
                                "mycharity" + ChrW(&h3002) + "org",
                                "prose" + ChrW(0) + "ware.com", "proseware..com", "a.org", 
                                "my_company.com" }
      Dim idn As New IdnMapping()
      
      For Each name In names
         Try
            Dim punyCode As String = idn.GetAscii(name)
            Dim name2 As String = idn.GetUnicode(punyCode)
            Console.WriteLine("{0} --> {1} --> {2}", name, punyCode, name2) 
            Console.WriteLine("Original: {0}", ShowCodePoints(name))
            Console.WriteLine("Restored: {0}", ShowCodePoints(name2))
         Catch e As ArgumentException 
            Console.WriteLine("{0} is not a valid domain name.", name)
         End Try
         Console.WriteLine()
      Next   
   End Sub
   
   Private Function ShowCodePoints(str1 As String) As String
      Dim output As String = ""
      For Each ch In str1
         output += String.Format("U+{0} ", Convert.ToUInt16(ch).ToString("X4"))
      Next
      Return output
   End Function
End Module
' The example displays the following output:
'    bücher.com --> xn--bcher-kva.com --> bücher.com
'    Original: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
'    Restored: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
'    
'    мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф
'    Original: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
'    Restored: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
'    
'    παράδειγμα.δοκιμή --> xn--hxajbheg2az3al.xn--jxalpdlp --> παράδειγμα.δοκιμή
'    Original: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
'    Restored: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
'    
'    mycharity。org --> mycharity.org --> mycharity.org
'    Original: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+3002 U+006F U+0072 U+0067
'    Restored: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+002E U+006F U+0072 U+0067
'    
'    prose ware.com is not a valid domain name.
'    
'    proseware..com is not a valid domain name.
'    
'    a.org --> a.org --> a.org
'    Original: U+0061 U+002E U+006F U+0072 U+0067
'    Restored: U+0061 U+002E U+006F U+0072 U+0067
'    
'    my_company.com --> my_company.com --> my_company.com
'    Original: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
'    Restored: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D

注解

Internet 域名由一个或多个部分组成,称为域名标签,由标签分隔符分隔。 例如,域名“www.proseware.com”由标签、“www”、“proseware”和“com”组成,用句点分隔。 标准域名由 US-ASCII(或基本拉丁语)字符范围中的指定字符组成,从 U+0021 到 U+007E。 为了促进不使用 US-ASCII 字符集的区域性中的 Internet 使用,2003 年采用了“应用程序(IDNA)中的域名国际化”标准,以支持将 Unicode 字符包含在 US-ASCII 字符范围之外。 但是,名称服务器和域名解析继续依赖于 US-ASCII 字符范围内的字符。

IDNA 机制使用 Punycode 将包含 US-ASCII 字符范围之外的 Unicode 字符的国际化域名映射到域名系统支持的 US-ASCII 字符范围。 IDNA 机制用于仅转换域名,而不是通过 Internet 传输的数据。

Important

在 .NET Framework 4.5 中, IdnMapping 该类支持不同版本的 IDNA 标准,具体取决于使用的操作系统:

请参阅 Unicode 技术标准 #46:IDNA 兼容性处理 ,了解这些标准处理特定字符集的方式的差异。

该方法 IdnMapping.GetAscii 规范化域名,将规范化名称转换为表示形式,该表示形式由 US-ASCII 代码点范围(U+0020 到 U+007E)中的可显示 Unicode 字符组成,并将 ASCII 兼容的编码(ACE)前缀(“xn--”)追加到每个标签。 该方法 IdnMapping.GetUnicode 还原方法 GetAscii 转换的域名标签。

如果要转换的字符串包括标签分隔符 IDEOGRAPHIC FULL STOP (U+3002)、FULLWIDTH FULL STOP (U+FF0E)和 HALFWIDTH IDEOGRAPHIC FULL STOP (U+FF61),则 GetAscii 该方法将它们转换为标签分隔符 FULL STOP(句点,U+002E)。 但是,该方法 GetUnicode 不会还原原始标签分隔符。

构造函数

名称 说明
IdnMapping()

初始化 IdnMapping 类的新实例。

属性

名称 说明
AllowUnassigned

获取或设置一个值,该值指示是否在当前 IdnMapping 对象的成员执行的操作中使用未分配的 Unicode 码位。

UseStd3AsciiRules

获取或设置一个值,该值指示在当前 IdnMapping 对象的成员执行的操作中使用标准命名约定还是宽松命名约定。

方法

名称 说明
Equals(Object)

指示指定的对象和当前 IdnMapping 对象是否相等。

GetAscii(String, Int32, Int32)

在域名标签的子字符串中对指定数量的字符进行编码,这些标签包含 US-ASCII 字符范围之外的 Unicode 字符。 子字符串转换为 US-ASCII 字符范围中可显示的 Unicode 字符字符串,并根据 IDNA 标准设置格式。

GetAscii(String, Int32)

对域名标签的子字符串进行编码,该标签包含 US-ASCII 字符范围之外的 Unicode 字符。 子字符串转换为 US-ASCII 字符范围中可显示的 Unicode 字符字符串,并根据 IDNA 标准设置格式。

GetAscii(String)

将由 Unicode 字符组成的域名标签字符串编码为 US-ASCII 字符范围内的可显示 Unicode 字符字符串。 该字符串根据 IDNA 标准进行格式设置。

GetHashCode()

返回此 IdnMapping 对象的哈希代码。

GetType()

获取当前实例的 Type

(继承自 Object)
GetUnicode(String, Int32, Int32)

将指定长度的子字符串解码为 Unicode 字符字符串,其中包含一个或多个域名标签(根据 IDNA 标准编码)。

GetUnicode(String, Int32)

将一个或多个域名标签的子字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

GetUnicode(String)

将一个或多个域名标签的字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
TryGetAscii(ReadOnlySpan<Char>, Span<Char>, Int32)

支持对 Internet 域名使用非 ASCII 字符。 无法继承此类。

TryGetUnicode(ReadOnlySpan<Char>, Span<Char>, Int32)

支持对 Internet 域名使用非 ASCII 字符。 无法继承此类。

适用于

线程安全性

所有公共方法 IdnMapping 都是线程安全的,并且可以从多个线程并发使用,前提是 IdnMapping 实例的属性未同时设置。

另请参阅