• Post Reply Bookmark Topic Watch Topic
  • New Topic

URL param encoding  RSS feed

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a URL in which the parameter values are unencoded, e.g.

http://groceryguide.com?gs=Bad Value

I need to change this to:

http://groceryguide.com?gs=Bad%20Value

Notice that I can't simply pass the whole string to URLEncoder.encode() because I only want to encode the parameter values. Is there a library available that can do this for me?

Cheers,
Dan
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URL in combination with URLEncoder can help you out.

URL has a method called getQuery which will return everything after the ?
Next you need to split that twice; first on & (so you will get separate parameter=value sets), next you split each of these on =. This will fail if there is a & or = inside a parameter value, but if that is the case the web server will be just as mistaken.

If possible I'd go back to the source though. How is this URL created?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!