4

From

Whats the java equivalent of Python’s urllib.urlencode?
Like

>>> urllib.urlencode({'abc':'d f', 'def': '-!2'})
'abc=d+f&def=-%212'

Where I can pass a HashMap of key values and it encodes and gives me the url string ..

Edit: I wanted to avoid this scenario

String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

of manually concatenating the strings with

=

and

&

1 Answer 1

3

java.net.URLEncoder should work for you - though you would have to extend it to accept the hashmap - but that is not very difficult.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes - you can avoid that by using a hashmap and a loop :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.