限制 .NET 中多字节字符的 URL 编码字符串的长度

发布于 2024-10-15 15:14:34 字数 220 浏览 4 评论 0原文

我正在使用 .NET,我需要截断可能包含多字节字符的字符串,以便在 URL 编码后它不会超过设定的长度。这似乎是内置的东西,但我找不到它。

一旦它被 URL 编码,我只会做一个子字符串,但这可能会去掉部分编码字符(空格变成 %20,如果它在末尾,它可能会被截断为 %2,这是无效的),或者多字节字符的一部分将被截断(π 被编码为 %CF%80,并且它可能被截断为 %、%CF、%CF%8,所有这些都是错误的)。

I am using .NET, and I need to truncate a string that may contain multibyte characters so that it will not be over a set length once it is URL encoded. This seems like something that would be built in, but I can't find it.

I would just do a substring once it is URL encoded, but that might take off part of a encoded character (space becomes %20, and if it was at the end it could get truncated to %2, which is invalid), or that part of a multibyte character would get truncated (π gets encoded as %CF%80, and it could get truncated as %, %CF, %CF%8, all of which are wrong).

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

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

发布评论

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

评论(1

疧_╮線 2024-10-22 15:14:34

我的快速谷歌搜索没有找到任何与此相关的内容,这有点令人惊讶,因为这似乎是一个相对常见的问题(至少对于那些不避免使用过长网址的人来说)。

您可以在对字符串进行编码的地方迭代地执行此操作,如果编码的字符串太长,您可以从原始字符串中删除一个字符并重新编码,并继续执行此操作,直到编码的字符串足够短。这显然不会有很好的性能。

My quick Google search didn't turn up anything for this, which is slightly surprising since this seems like a relatively common problem (at least for those who don't avoid monstrously long URLs).

You could do this iteratively where you encode the string, and if the encoded string is too long you chop a character off the original and re-encode, and keep doing this until the encoded string is short enough. This would obviously not be very performant.

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