aspose file tools
The moose likes Java in General and the fly likes URLEncoder class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "URLEncoder class ?" Watch "URLEncoder class ?" New topic
Author

URLEncoder class ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
what does this method static encode() of the class URLEncoder do ?
what is this translation of a string into x-www-form-urlencoded ??
in one of the examples its usage is like this:
String ss = URLEncoder.encode(args[0]);// the user enters some string at command line
pw.println("string is : "+ss);//where pw is PrintWriter object
here is the api
encode(String s)
Translates a string into x-www-form-urlencoded format.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2573

It is meant for URL encoding the string. When URLs contain a query string, (name=value pairs), the values are encoded using this method. URL encoding includes converting spaces to + or %20 and all other non alpha-numeric characters are converted to their HEX value, which is of the form - %XX, where XX is the HEX value. So my name Manish Hatwalne becomes either Manish+Hatwalne or Manish%20Hatwalne. This can also safely pass values such as /, // and . (period) which have special meaning in the URL.
Of course, there's a corresponding decode method for this. I believe this information is documented in the API as well.
HTH,
- Manish
Ed Bicker
Greenhorn

Joined: Oct 14, 2003
Posts: 12
Yes, but why do you need to URLEncode a string, in the first place. What good does it serve?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You need to URL encode Strings to put them on a URL, otherwise you could add data that acts strangely or creates illegal URLs.

eg it fixes this:


and it stops this behaving badly:


Dave
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
Originally posted by Ed Bicker:
Yes, but why do you need to URLEncode a string, in the first place. What good does it serve?

You would normally not need them because browser does this encoding when it issues a GET request. But in case you are using applets and issuing the GET request programmatically, then you would have to encode() them before it reaches the server.


Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLEncoder class ?
 
Similar Threads
Mock Exam Question
JavaScript/java equivalent function?
"application/x-www-form-urlencoded"
URL connection problem
passing string with href..(URGENT)