简体   繁体   English

URL将数据编码为'%xx'的C#方法

[英]C# Method to URL encode data to '%xx'

I am in a situation where i have to replace some Windows-1252 special characters. 我处于必须替换一些Windows-1252特殊字符的情况。 More characters may be added later - so i want a general method instead of this hardcoding. 以后可能会添加更多字符-因此,我希望使用通用方法代替此硬编码。

private string URLEncode(string s)
        {
            s = s.Replace("ø", "%f8");
            s = s.Replace("Ø", "%f8");
            s = s.Replace("æ", "%e6");
            s = s.Replace("Æ", "%e6");
            s = s.Replace("å", "%e5");
            s = s.Replace("Å", "%e5");
            return s;
        }

Is this possible? 这可能吗? I don't know what this '%f8' format is, but it works. 我不知道这种'%f8'格式是什么,但是它可以工作。

Okay, now I get your problem. 好的,现在我明白了。 I think I found a suitable solution in another post: 我想我在另一篇文章中找到了合适的解决方案:

UrlEncoding issue for string with ß character 带有ß字符的字符串的UrlEncoding问题

Which in your case could be: 在您的情况下,可能是:

var source = "tØst".ToLowerInvariant();
var encodedUrl = HttpUtility.UrlEncode(source, Encoding.GetEncoding(1252));

I had to make the string lowercase to better match your current mapping. 我必须将字符串小写以更好地匹配您当前的映射。

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

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