开发者

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

开发者 https://www.devze.com 2022-12-17 12:48 出处:网络
From Whats the java equivalent of Python’s urllib.urlencode? Like >>> urllib.urlencode({\'abc\':\'d f\', \'def\': \'-!2\'})

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

&


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号