如何对 URL 中的加号 (+) 进行编码 [英] How to encode the plus (+) symbol in a URL

查看:54
本文介绍了如何对 URL 中的加号 (+) 进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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&to=someemail@somedomain.com&su=some subject&body=你好+你好

https://mail.google.com/mail?view=cm&tf=0&to=someemail@somedomain.com&su=some subject&body=Hi there+Hello there

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

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

推荐答案

+ 字符在 URL 中具有特殊含义 =>这意味着空格 - .如果要使用文字 + 符号,则需要将其 URL 编码为 %2b:

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

body=Hi+there%2bHello+there

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

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"] = "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

这篇关于如何对 URL 中的加号 (+) 进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆