Java equivalent of Pythons urllib.urlencode(HashMap based UrlEncode)

醉酒当歌 提交于 2019-12-21 20:30:24

问题


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:


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



来源:https://stackoverflow.com/questions/2088502/java-equivalent-of-pythons-urllib-urlencodehashmap-based-urlencode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!