下面的 URL 链接将打开一个新的 Google 邮件窗口。我遇到的问题是 Google 将电子邮件正文中的所有加号 (+) 替换为空格。看起来只有 +
符号才会发生这种情况。我该如何补救? (我正在开发 ASP.NET 网页。)
https://mail.google.com/mail?view=cm&tf=0&to=someemail@somedomain.com&su=some subject&body=你好+你好
(在正文电子邮件中,“Hithere+Hellothere”将显示为“HithereHellothere”)
最佳答案
+
字符在 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"] = "someemail@somedomain.com";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";
uriBuilder.Query = values.ToString();
Console.WriteLine(uriBuilder.ToString());
结果:
关于asp.net - 如何对 URL 中的加号 (+) 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450190/