如何对 URL 中的加号 (+) 进行编码
下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
+
字符在 URL [的查询段] 中具有特殊含义 =>它意味着空白:。如果您想在此处使用文字
+
符号,则需要将其 URL 编码为%2b
:以下是如何在 .NET 中正确生成 URL 的示例
:结果:
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
:Here's an example of how you could properly generate URLs in .NET:
The result:
如果您想在正文中使用加号
+
符号,则必须将其编码为%2B
。例如:
试试这个
If you want a plus
+
symbol in the body you have to encode it as%2B
.For example:
Try this
为了使用 JavaScript 对
+
值进行编码,您可以使用encodeURIComponent
函数。例子:
In order to encode a
+
value using JavaScript, you can use theencodeURIComponent
function.Example:
只需将其添加到列表中:
请参阅 https://stackoverflow.com/a/34189188/98491
通常您想要使用
EscapeDataString
就可以了。Just to add this to the list:
See https://stackoverflow.com/a/34189188/98491
Usually you want to use
EscapeDataString
which does it right.始终对除 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.
一般来说,如果您使用 .NET API -
new Uri("someproto:with+plus").LocalPath
或AbsolutePath
将在 URL 中保留加号字符。 (相同的"someproto:with+plus"
字符串),但
Uri.EscapeDataString("with+plus")
将转义加号字符并生成"with%2Bplus “
。为了保持一致,我建议始终将加号字符转义为
"%2B"
并在任何地方使用它 - 然后无需猜测谁在想以及您的加号字符如何。我不确定为什么转义字符
'+'
解码会产生空格字符' '
- 但显然这是某些组件的问题。Generally if you use .NET API's -
new Uri("someproto:with+plus").LocalPath
orAbsolutePath
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.使用encodeURLComponent
Use encodeURLComponent