Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding parameters for a URL

I have a Silverlight application that is building a URL. This URL is a call to a REST-based service. This service expects a single parameter that represents a location. The location is in the form of "city, state". To build this URL, I'm calling the following code:

string url = "http://www.example.com/myService.svc/"; url += HttpUtility.UrlEncode(locationTextBox.Text); 

If a user enters "chicago, il" into locationTextBox, the result looks like this:

http://www.example.com/myService.svc/chicago%2c+il 

In reality though, I was kind of expecting the URL to look like;

http://www.example.com/myService.svc/chicago,%20il 

When testing my service via the browser URL, the one I am expecting works. However, the URL that is being generated is not working. What am I doing wrong?

like image 319
user70192 Avatar asked Feb 23 '11 18:02

user70192


People also ask

Should URL parameters be encoded?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

Are URLs UTF-8 or ASCII?

The UTF-8 locale support addresses this need. Browsers are limited to a defined character set that can legally be used in a uniform resource locator (URL). This range is defined to be the printable characters in the ASCII character set (between hex code 0x20 and 0x7e).

What is a URL parameter?

URL parameter is a way to pass information about a click through its URL. You can insert URL parameters into your URLs so that your URLs track information about a click. URL parameters are made of a key and a value separated by an equals sign (=) and joined by an ampersand (&).

What does %20 in a URL mean?

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.


1 Answers

I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.

like image 73
Alexei Levenkov Avatar answered Oct 05 '22 18:10

Alexei Levenkov