将 url 从 utf-8 编码重新编码为 iso-8859-1 编码

发布于 2024-08-15 20:56:13 字数 743 浏览 7 评论 0原文

我有 file:// 链接,其中包含非英语字符,这些字符以 UTF-8 进行 UrlEncoded。为了使这些链接在浏览器中工作,我必须对它们重新编码。

file://development/H%C3%A5ndplukket.doc

变成

file://development/H%e5ndplukket.doc

我有以下有效的代码:

public string ReEncodeUrl(string url)
{
    Encoding enc = Encoding.GetEncoding("iso-8859-1");
    string[] parts = url.Split('/');
    for (int i = 1; i < parts.Length; i++)
    {
        parts[i] = HttpUtility.UrlDecode(parts[i]); // Decode to string
        parts[i] = HttpUtility.UrlEncode(parts[i], enc); // Re-encode to latin1
        parts[i] = parts[i].Replace('+', ' '); // Change + to [space]
    }
    return string.Join("/", parts);
}

有没有更干净的方法来做到这一点?

I have file:// links with non-english characters which are UrlEncoded in UTF-8. For these links to work in a browser I have to re-encode them.

file://development/H%C3%A5ndplukket.doc

becomes

file://development/H%e5ndplukket.doc

I have the following code which works:

public string ReEncodeUrl(string url)
{
    Encoding enc = Encoding.GetEncoding("iso-8859-1");
    string[] parts = url.Split('/');
    for (int i = 1; i < parts.Length; i++)
    {
        parts[i] = HttpUtility.UrlDecode(parts[i]); // Decode to string
        parts[i] = HttpUtility.UrlEncode(parts[i], enc); // Re-encode to latin1
        parts[i] = parts[i].Replace('+', ' '); // Change + to [space]
    }
    return string.Join("/", parts);
}

Is there a cleaner way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风不识月 2024-08-22 20:56:14

虽然我没有看到任何真正的改变它的方法会产生影响,但空格替换的 + 不应该在 UrlEncode 之前,这样它就会变成 %20 吗?

While I don't see any real way of changing it that would make a difference, shouldn't the + to space replace be before you UrlEncode so it turns into %20?

挥剑断情 2024-08-22 20:56:14

诚然丑陋,并不是真正的改进,但可以重新编码整个事情(避免拆分/迭代/连接)然后 .Replace("%2f", "/")

我不明白想要保留空格的代码在最终结果中 - 似乎如果其中仍然有空格,您最终不会得到实际编码的东西?

admittedly ugly and not really an improvement, but could re-encode the whole thing (avoid the split/iterate/join) then .Replace("%2f", "/")

I don't understand the code wanting to keep a space in the final result - seems like you don't end up with something that's actually encoded if it still has spaces in it?

仅冇旳回忆 2024-08-22 20:56:13

我认为这实际上非常干净。它是可读的,并且你说它功能正常。只要实现对消费者隐藏,我就不会担心挤出最后的改进。

如果您过度执行此操作(例如每个事件数百次执行),我会考虑将实现从 UrlEncode/UrlDecode 中取出,并将它们相互流式传输,以通过消除字符串拆分/连接的需要来提高性能,但是无论如何,测试都必须证明这一点,并且绝对不会是“干净的”:-)

I think that's pretty clean actually. It's readable and you said it functions correctly. As long as the implementation is hidden from the consumer, I wouldn't worry about squeezing out that last improvement.

If you are doing this operation excessively (like hundreds of executions per event) I would think about taking the implementation out of UrlEncode/UrlDecode and stream them into each other to get a performance improvement there by removing the need for string split/join, but testing would have to prove that out anyway and definitely wouldn't be "clean" :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文