asp.net - 如何对 URL 中的加号 (+) 进行编码

标签 asp.net c#-4.0 gmail urlencode html-encode

下面的 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());

结果:

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

关于asp.net - 如何对 URL 中的加号 (+) 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450190/

相关文章:

.net - 在 ASP.NET 中,当 HttpContext.Current 为 NULL 时如何获取物理文件路径?

asp.net - 为什么 document.getElementById ('hyperlink_element_id' ) 返回超链接的 href 属性的值?

c# - 检查对象是否为 ObservableCollection?

Python:IMAP 连接到 gmail 返回错误

c# - ASP.Net c# 选择了给定 GroupName 中的哪个单选按钮?

c# - IHttpContextAccessor 在类中为 null

c# - 记录 C# 代码的规则/指南?

c# - 设置触发从父模板 (WPF) 传递下来的 'self' 属性

ios - Google 登录在允许后重定向到 google.com 而不是应用程序

email - SMTP 错误 : 534 when sending email through gmail in golang app