简体   繁体   English

适用于Windows应用商店的.NET:UrlEncode

[英].NET for Windows Store Apps: UrlEncode

In some Windows Store App I need send URL like 在某些Windows Store应用中,我需要发送如下网址

new Uri(string.Format(@"http://www.site.com?word={0}",
                                      sourceText))

, where sourceText is the escaped representation of some text. ,其中sourceText是某些文本的转义表示。 It would be easy, if I need UTF-8, but I need Windows-1251 encoding. 如果我需要UTF-8,但是我需要Windows-1251编码,那将很容易。

I have tried 我努力了

    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));

but Uri.EscapeUriString use string , which converting to Unicode automatically. 但是Uri.EscapeUriString使用string ,它会自动转换为Unicode。

I could use HttpUtility.UrlEncode(word, Encoding.GetEncoding(1251) , but there is no System.Web.HttpUtility in .NET for Windows Store. 我可以使用HttpUtility.UrlEncode(word, Encoding.GetEncoding(1251) ,但是.NET中没有适用于Windows Store的System.Web.HttpUtility

For example, 'привет' in UTF-8: %D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82 ; 例如,UTF-8中的“привет”: %D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82 in Windows-1251 : %EF%F0%E8%E2%E5%F2 . 在Windows-1251中: %EF%F0%E8%E2%E5%F2 I need second string 我需要第二根弦

How about WebUtility.UrlEncode ? WebUtility.UrlEncode怎么WebUtility.UrlEncode

It lives in the System.Net namespace, more info here: http://msdn.microsoft.com/en-us/library/system.net.webutility.urlencode.aspx 它位于System.Net命名空间中,在此处有更多信息: http : //msdn.microsoft.com/zh-cn/library/system.net.webutility.urlencode.aspx

string sourceText = "привет";
byte[] win1251bytes = Encoding.GetEncoding("windows-1251").GetBytes(sourceText);
string hex = BitConverter.ToString(win1251bytes);
string result = "%" + hex.Replace("-", "%");
// Result: %EF%F0%E8%E2%E5%F2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM