如何对 URL 中的加号 (+) 进行编码

发布于 2024-10-26 10:19:19 字数 542 浏览 0 评论 0原文

下面的 URL 链接将打开一个新的 Google 邮件窗口。我遇到的问题是 Google 将电子邮件正文中的所有加号 (+) 替换为空格。看起来只有 + 符号才会发生这种情况。我该如何补救? (我正在处理 ASP.NET 网页。)

https://mail.google.com/mail?view=cm&tf=0&[电子邮件受保护] &su=some subject&body=Hithere+Hellothere

(在正文电子邮件中,“Hithere+Hellothere”将显示为“HithereHellothere”)

The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) signs in the email body with blank space. It looks like it only happens with the + sign. How can I remedy this? (I am working on a ASP.NET web page.)

https://mail.google.com/mail?view=cm&tf=0&[email protected]&su=some subject&body=Hi there+Hello there

(In the body email, "Hi there+Hello there" will show up as "Hi there Hello there")

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

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

发布评论

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

评论(7

旧街凉风 2024-11-02 10:19:19

+ 字符在 URL [的查询段] 中具有特殊含义 =>它意味着空白:。如果您想在此处使用文字 + 符号,则需要将其 URL 编码为 %2b

body=Hi+there%2bHello+there

以下是如何在 .NET 中正确生成 URL 的示例

var uriBuilder = new UriBuilder("https://mail.google.com/mail");

var values = HttpUtility.ParseQueryString(string.Empty);
values["view"] = "cm";
values["tf"] = "0";
values["to"] = "[email protected]";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";

uriBuilder.Query = values.ToString();

Console.WriteLine(uriBuilder.ToString());

:结果:

https://mail.google.com:443/mail?view=cm&tf=0&to=someemail%40somedomain.com&su=some+subject&body=Hi+那里%2bHello+那里

The + character has a special meaning in [the query segment of] a URL => it means whitespace: . If you want to use the literal + sign there, you need to URL encode it to %2b:

body=Hi+there%2bHello+there

Here's an example of how you could properly generate URLs in .NET:

var uriBuilder = new UriBuilder("https://mail.google.com/mail");

var values = HttpUtility.ParseQueryString(string.Empty);
values["view"] = "cm";
values["tf"] = "0";
values["to"] = "[email protected]";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";

uriBuilder.Query = values.ToString();

Console.WriteLine(uriBuilder.ToString());

The result:

https://mail.google.com:443/mail?view=cm&tf=0&to=someemail%40somedomain.com&su=some+subject&body=Hi+there%2bHello+there

野味少女 2024-11-02 10:19:19

如果您想在正文中使用加号 + 符号,则必须将其编码为 %2B

例如:
试试这个

If you want a plus + symbol in the body you have to encode it as %2B.

For example:
Try this

所有深爱都是秘密 2024-11-02 10:19:19

为了使用 JavaScript 对 + 值进行编码,您可以使用 encodeURIComponent 函数。

例子:

var url = "+11";
var encoded_url = encodeURIComponent(url);
console.log(encoded_url)

In order to encode a + value using JavaScript, you can use the encodeURIComponent function.

Example:

var url = "+11";
var encoded_url = encodeURIComponent(url);
console.log(encoded_url)

a√萤火虫的光℡ 2024-11-02 10:19:19

只需将其添加到列表中:

Uri.EscapeUriString("Hi there+Hello there") // Hi%20there+Hello%20there
Uri.EscapeDataString("Hi there+Hello there") // Hi%20there%2BHello%20there

请参阅 https://stackoverflow.com/a/34189188/98491

通常您想要使用 EscapeDataString 就可以了。

Just to add this to the list:

Uri.EscapeUriString("Hi there+Hello there") // Hi%20there+Hello%20there
Uri.EscapeDataString("Hi there+Hello there") // Hi%20there%2BHello%20there

See https://stackoverflow.com/a/34189188/98491

Usually you want to use EscapeDataString which does it right.

墨小墨 2024-11-02 10:19:19

始终对除 RFC-3986 中定义为“未保留”的字符之外的所有字符进行百分比编码会更安全。

未保留=字母/数字/“-”/“。” / "_" / "~"

因此,对加号字符和其他特殊字符进行百分比编码。

您遇到的优点问题是因为根据 RFC-1866(HTML 2.0 规范)第 8.2.1 段。第1小段,“表单字段名称和值被转义:空格字符被‘+’替换,然后保留字符被转义”)。这种表单数据的编码方式在后面的HTML规范中也给出了,查找application/x-www-form-urlencoded的相关段落。

It's safer to always percent-encode all characters except those defined as "unreserved" in RFC-3986.

unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

So, percent-encode the plus character and other special characters.

The problem that you are having with pluses is because, according to RFC-1866 (HTML 2.0 specification), paragraph 8.2.1. subparagraph 1., "The form field names and values are escaped: space characters are replaced by `+', and then reserved characters are escaped"). This way of encoding form data is also given in later HTML specifications, look for relevant paragraphs about application/x-www-form-urlencoded.

樱花落人离去 2024-11-02 10:19:19

一般来说,如果您使用 .NET API - new Uri("someproto:with+plus").LocalPathAbsolutePath 将在 URL 中保留加号字符。 (相同的 "someproto:with+plus" 字符串)

,但 Uri.EscapeDataString("with+plus") 将转义加号字符并生成 "with%2Bplus “

为了保持一致,我建议始终将加号字符转义为 "%2B" 并在任何地方使用它 - 然后无需猜测谁在想以及您的加号字符如何。

我不确定为什么转义字符 '+' 解码会产生空格字符 ' ' - 但显然这是某些组件的问题。

Generally if you use .NET API's - new Uri("someproto:with+plus").LocalPath or AbsolutePath will keep plus character in URL. (Same "someproto:with+plus" string)

but Uri.EscapeDataString("with+plus") will escape plus character and will produce "with%2Bplus".

Just to be consistent I would recommend to always escape plus character to "%2B" and use it everywhere - then no need to guess who thinks and what about your plus character.

I'm not sure why from escaped character '+' decoding would produce space character ' ' - but apparently it's the issue with some of components.

请叫√我孤独 2024-11-02 10:19:19

使用encodeURLComponent

const email = "[email protected]"
const encodedEmail = encodeURIComponent(email)
const newurl = `https://something.com/test?email=${encodedEmail}`

Use encodeURLComponent

const email = "[email protected]"
const encodedEmail = encodeURIComponent(email)
const newurl = `https://something.com/test?email=${encodedEmail}`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文