如何编码URL中的加号(+)符号

use*_*234 55 asp.net gmail html-encode urlencode c#-4.0

下面的URL链接将打开一个新的Google邮件窗口.我遇到的问题是Google用空格替换了电子邮件正文中的所有加号(+).看起来它只发生在+符号上.有关如何解决这个问题的任何建议?(我正在使用ASP.NET网页)

https://mail.google.com/mail?view=cm&tf=0&to=someemail@somedomain.com&su=some subject&body =你好+那里你好

(在正文电子邮件中,"你好+你好那里"将显示为"你好你好那里")

Dar*_*rov 86

+字符在url中具有特殊含义=>它表示空格.如果您想使用+您需要对其进行URL编码的符号:

body=Hi+there%2bHello+there
Run Code Online (Sandbox Code Playgroud)

以下是如何在.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"] = "someemail@somedomain.com";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";

uriBuilder.Query = values.ToString();

Console.WriteLine(uriBuilder.ToString());
Run Code Online (Sandbox Code Playgroud)

结果

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

  • 该RFC清楚地说,+符号可用于未编码,并且如果它必须被编码,没有理由把它变成一个"空间"字符.也许你可以向我们指出一个标准的正确文件,提到将+符号转换为空格符号的内容,反之亦然. (3认同)
  • 是的你在说什么?我从未见过RFC或任何说+表示空格的东西.... (3认同)
  • @PabloAriel 根据 [W3C 文档寻址 URL](https://www.w3.org/Addressing/URL/uri-spec.html):“在查询字符串中,加号被保留为空格的简写符号。因此,必须对真实的加号进行编码。该方法用于使查询 URI 更容易在不允许空格的系统中传递。” (3认同)
  • **警告:** 如果你使用 `UriBuilder` 的 `Uri` 属性,你会得到一个糟糕的结果。示例中的 `uriBuilder.Uri.ToString()` 将返回 `Hi+there+Hello+there`。使用 `uriBuilder.Uri.AbsoluteUri` 似乎可以正常工作,http://stackoverflow.com/a/15120429/1931573 表明这已在 .NET 4.5 中修复。就 RFC 而言,HTML 4 规范说 URL 查询字符串的类型是“application/x-www-form-urlencoded”,它本身将 (+) 定义为空间。所以它不是 RFC,而是 HTML 标准的一部分。 (2认同)
  • 请注意,IIS 认为这是“双重编码”,并且经常会阻止以此方式加载的 url,并显示以下错误: HTTP 错误 404.11 - 未找到 请求过滤模块配置为拒绝包含双重转义序列的请求。 (2认同)

Bla*_*rog 17

你想在体内加一个加号(+),你必须将其编码为2B.

例如: 试试这个

  • 字面意思是“%2B”? (2认同)

小智 9

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

例子:

var url = "+11";
var encoded_url = encodeURIComponent(url);
console.log(encoded_url)
Run Code Online (Sandbox Code Playgroud)


Jür*_*ock 5

只需将其添加到列表中即可:

Uri.EscapeUriString("Hi there+Hello there") // Hi%20there+Hello%20there
Uri.EscapeDataString("Hi there+Hello there") // Hi%20there%2BHello%20there
Run Code Online (Sandbox Code Playgroud)

请参阅/sf/answers/2393243191/

通常你想使用EscapeDataString哪个才是正确的。