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

查看:227
本文介绍了如何在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 主题& 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());

结果

查看全文

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