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) 标准,以支持在 US-ASCII 字符范围之外包含 Unicode 字符。 但是,名称服务器和域名解析继续依赖于 US-ASCII 字符范围内的字符。

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

重要

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

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

方法 IdnMapping.GetAscii 规范化域名,将规范化名称转换为由 U+0020 到 U+007E) 的 US-ASCII 码位 (范围中可显示的 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)

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

GetAscii(String, Int32)

编码包含US-ASCII字符范围以外的 Unicode 字符的域名称标签子字符串。 子串转换为在 US-ASCII 字符范围内可显示的“ Unicode ”字符串并根据 IDNA 标准格式化。

GetAscii(String, Int32, Int32)

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

GetHashCode()

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

GetType()

获取当前实例的 Type

(继承自 Object)
GetUnicode(String)

对基于 IDNA 标准编码的一个或者多个域名标签的字符串进行解码,解码为一个 Unicode 字符串。

GetUnicode(String, Int32)

对基于 IDNA 标准编码的一个或者多个域名标签的子字符串进行解码,解码为 Unicode 字符串。

GetUnicode(String, Int32, Int32)

对基于 IDNA 标准编码、具有指定长度并包含一个或者多个域名标签的子字符串进行解码,解码为一个 Unicode 字符串。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

线程安全性

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

另请参阅