在某些Windows Store应用,我需要发送URL一样.NET为Windows应用商店的应用程序:以UrlEncode
new Uri(string.Format(@"http://www.site.com?word={0}",
sourceText))
,其中sourceText
是一些文本的转义表示。如果我需要UTF-8,这很容易,但我需要Windows-1251编码。
我已经试过
byte[] unicodeBytes = Encoding.Unicode.GetBytes(sourceText);
byte[] win1251bytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding("windows-1251"), unicodeBytes);
string sourceText =
Uri.EscapeUriString(Encoding.GetEncoding("windows-1251").GetString(win1251bytes, 0, win1251bytes.Length));
但Uri.EscapeUriString
使用string
,它会自动转换为Unicode。
我可以使用HttpUtility.UrlEncode(word, Encoding.GetEncoding(1251)
,但在Windows应用商店的.NET中没有System.Web.HttpUtility
。
例如,UTF-8中的'привет':%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82
;在Windows-1251中的 :%EF%F0%E8%E2%E5%F2
。我需要第二个字符串