IdnMapping
使用IdnMapping获取中文域名的Punycode码
public static string Punycode(this string str){if (string.IsNullOrWhiteSpace(str) || !IsContainChinese(str))return str;var idn = new IdnMapping();var url = idn.GetAscii(str);return url;}
测试
var chineseUrl = "中文.中国";var pu = chineseUrl.Punycode();Console.WriteLine(pu);// 输出 xn--fiq228c.xn--fiqs8s
tip: 中文域名前加http/https转码会乱
var chineseUrl = "http://中文.中国";var pu = chineseUrl.Punycode();Console.WriteLine(pu);//输出 : xn--http://-m43kr94o.xn--fiqs8s