How to encode the plus (+) symbol in URL

对着背影说爱祢 提交于 2020-01-19 03:09:19

问题


The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) sign in the email body with blank space. It looks like it only happens with the + sign. Any suggestions on how to remedy this? ( I am working the ASP.NET web page)

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")


回答1:


The + character has a special meaning in a url => it means whitespace. If you want to use the + sign you need to URL encode it:

body=Hi+there%2bHello+there

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());

The result

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




回答2:


Is you want a plus (+) symbol in the body you have to encode it as 2B.

For example: Try this




回答3:


for javascript language use encodeURIComponent function to encode special characters




回答4:


It's safer to always percent-encode all characters except those defined as "unreserved" in RFC-3986.

unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

So, percent-encode the plus character and other special characters.

The problem that you are having with pluses is because, according to RFC-1866 (HTML 2.0 specification), paragraph 8.2.1. subparagraph 1., "The form field names and values are escaped: space characters are replaced by `+', and then reserved characters are escaped"). This way of encoding form data is also given in later HTML specifications, look for relevant paragraphs about application/x-www-form-urlencoded.



来源:https://stackoverflow.com/questions/5450190/how-to-encode-the-plus-symbol-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!