-1

I hope to encode a string to a url, but URLEncoder.encode() cannot do it quite well:

URLEncoder.encode("http://www.example.com/1/hello world", "utf8")

will result in

http%3A%2F%2Fwww.example.com%2F1%2Fhello+world

What I hope to get is:

http://www.example.com/1/hello+world

without encoding the / and : characters.


EDIT

This is a just a simple example here, actually I have many non-ascii characters in the url.

Yishu Fang
  • 9,448
  • 21
  • 65
  • 102

1 Answers1

0

you can convert "%3A" to ":" and convert "%2F" to "/" after encode. eg:

String ret = URLEncoder.encode("http://www.example.com/1/hello world", "utf8");
String ret2 = ret.replace("%3A", ":").replace("%2F", "/");

ret2 is what you want..

micneer
  • 16
  • 3