Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url encoding quotes and spaces

I have some query text that is being encoded with JavaScript, but I've encountered a use case where I might have to encode the same text on the server side, and the encoding that's happening is not the same. I need it to be the same. Here's an example.

I enter "I like food" into the search box and hit the search button. JavaScript encodes this as %22I%20like%20food%22

Let's say I get the same value as a string on a request object on the server side. It will look like this: "\"I like food\""

When I use HttpUtility.UrlEncode(value), the result is "%22I+like+food%22". If I use HttpUtility.UrlPathEncode(value), the result is "\"I%20like%20food\""

So UrlEncode is encoding my quotes but is using the + character for spaces. UrlPathEncode is encoding my spaces but is not encoding my escaped quotes.

I really need it to do both, otherwise the Search code completely borks on me (and I have no control over the search code).

Tips?

like image 709
Samo Avatar asked Jan 27 '11 18:01

Samo


People also ask

Why is space %20 in URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.

Are double quotes allowed in URLs?

Show activity on this post. I know that the double quote character is not allowed in the url and it is encoded as %22 and this is done with utf-8 encoding .

What characters should be URL encoded?

Special characters needing encoding are: ':' , '/' , '?' , '#' , '[' , ']' , '@' , '!' , '$' , '&' , "'" , '(' , ')' , '*' , '+' , ',' , ';' , '=' , as well as '%' itself.


1 Answers

UrlPathEncode doesn't escape " because they don't need to be escaped in path components.

Uri.EscapeDataString should do what you want.

like image 75
porges Avatar answered Sep 23 '22 23:09

porges