I need to encode the parameters values in an URL. If I use following:
URLEncoder.encode(url, "UTF-8");
for a URL like this: http://localhost:8080/...
it will encode "://" etc. What I need is the encoding only for the values of the parameters starting from all the URL string. So in this case:
http://localhost/?q=blah&d=blah
I want encoded only the "blah" in the 2 parameters values (for n parameters of course).
What's your best way?
Thanks
Randomize
You're using URLEncoder in a wrong way. You're supposed to encode every parameter value separately and then assemble the url together.
For example
String url = "http://localhost/?q=" + URLEncoder.encode ("blah", "UTF-8") + "&d=" + URLEncoder.encode ("blah", "UTF-8");
For a URL like http://localhost/hello/sample?param=bla, you could use this (from Java's java.net.URI class):
URI uri = new URI("http", "localhost", "/hello/sample", "param=bla", null);
String url = uri.toASCIIString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With