How to encode a plus sign (+) in a url

The URL link below will open a new Google email window. The problem is that Google is replacing all the pluses (+) in the email element with empty space. It seems that this happens only with the + sign. Any suggestions on how to fix this? (I work on an ASP.NET webpage)

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

(In the body email, “Hello there + Hello there” will appear as “Hello there Hello there”)

+48
html-encode urlencode gmail
Mar 27 '11 at 15:27
source share
4 answers

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

 body=Hi+there%2bHello+there 

Here is an example of how you could correctly 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()); 

Result

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

+77
Mar 27 '11 at 15:32
source share

You need a plus sign (+) in the body, which you must encode as 2B.

For example: Try this

+16
Mar 27 '11 at 15:33
source share

It is safer to always encode the percentages of all characters except those defined as “unconditionally” in RFC-3986.

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

So, percentages encode the plus sign and other special characters.

The problem you are experiencing with the pluses is that in accordance with RFC-1866 (HTML 2.0 specification), clause 8.2.1. subparagraph 1. "The names and values ​​of the form fields are reset: the space characters are replaced with" + ", and then the reserved characters are escaped"). This method of encoding form data is also provided in later HTML specifications; look for the relevant paragraphs about the application / x-www-form-urlencoded.

+2
Oct 27 '16 at 19:17
source share

for JavaScript, use the encodeURIComponent function to encode special characters

0
Jan 12 '19 at 6:28
source share



All Articles