1

I have an url, that contains Russian symbols and for encoding/decoding I use URIUtil.encodeQuery/decode. For example this piece of code

String url =  
"https://dom.sakh.com/flat/sell?s[layout][]=102 серия&s[layout][]=138 серия&s[layout][]=306 серия&s[layout][]=97 серия&s[layout][]=97-С серия&s[layout][]=брежневка&s[layout][]=гостинка&s[layout][]=индивидуальная&s[layout][]=новая пл.&s[layout][]=сталинка&s[layout][]=хрущевка&s[price][min]=0&s[price][max]=10000000000";


System.out.println(URIUtil.encodeQuery(url));

It will display the following string

https://dom.sakh.com/flat/sell?s%5Blayout%5D%5B%5D=102%20%D1%81%D0%B5%D1%80%D0%B8%D1%8F&s%5Blayout%5D%5B%5D=138%20%D1%81%D0%B5%D1%80%D0%B8%D1%8F&s%5Blayout%5D%5B%5D=306%20%D1%81%D0%B5%D1%80%D0%B8%D1%8F&s%5Blayout%5D%5B%5D=97%20%D1%81%D0%B5%D1%80%D0%B8%D1%8F&s%5Blayout%5D%5B%5D=97-%D0%A1%20%D1%81%D0%B5%D1%80%D0%B8%D1%8F&s%5Blayout%5D%5B%5D=%D0%B1%D1%80%D0%B5%D0%B6%D0%BD%D0%B5%D0%B2%D0%BA%D0%B0&s%5Blayout%5D%5B%5D=%D0%B3%D0%BE%D1%81%D1%82%D0%B8%D0%BD%D0%BA%D0%B0&s%5Blayout%5D%5B%5D=%D0%B8%D0%BD%D0%B4%D0%B8%D0%B2%D0%B8%D0%B4%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F&s%5Blayout%5D%5B%5D=%D0%BD%D0%BE%D0%B2%D0%B0%D1%8F%20%D0%BF%D0%BB.&s%5Blayout%5D%5B%5D=%D1%81%D1%82%D0%B0%D0%BB%D0%B8%D0%BD%D0%BA%D0%B0&s%5Blayout%5D%5B%5D=%D1%85%D1%80%D1%83%D1%89%D0%B5%D0%B2%D0%BA%D0%B0&s%5Bprice%5D%5Bmin%5D=0&s%5Bprice%5D%5Bmax%5D=5000000000

And if I apply a method URIUtil.decode to this url i will get back:

https://dom.sakh.com/flat/sell?s[layout][]=102 серия&s[layout][]=138 серия&s[layout][]=306 серия&s[layout][]=97 серия&s[layout][]=97-С серия&s[layout][]=брежневка&s[layout][]=гостинка&s[layout][]=индивидуальнаяs[layout][]=новая пл.&s[layout][]=сталинка&s[layout][]=хрущевка&s[price][min]=0&s[price][max]=10000000000

But these methods are a bit old (httpclient's (3.1) URIUtil.encodeQuery() is gone in org.apache.httpcomponents (4.4.1) and i am trying to replace thsese methods). So my question is how to change my code saving the behavior? What to use? Provide an example, please, if it is possible.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Welcome to stack overflow, Please go through the [tour](https://stackoverflow.com/tour) so you get batter idea of How this site works, the help center and the [How to Ask](https://stackoverflow.com/questions/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. Please also have a look at [How do I ask](http://meta.stackexchange.com/a/10812/162852) – Dushyant Tankariya Oct 03 '19 at 13:35
  • "These methods are a bit old", why does that matter? The relevant RFC hasn't changed, nor does code lose its functionality with age. – Mark Rotteveel Oct 03 '19 at 13:40
  • @MarkRotteveel I mean httpclient's (3.1) URIUtil.encodeQuery() has gone in org.apache.httpcomponents (4.4.1), so i am trying to replace this code – Vladimir Safonov Oct 03 '19 at 13:45
  • Please explicitly mention that in your question. – Mark Rotteveel Oct 03 '19 at 13:46
  • You most likely want to use [URLEncoder](https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html) – Aaron Oct 03 '19 at 14:22
  • @Aaron URLEncoder will replace special characters like '&' and '=', but it is not the behaviour I am waiting for – Vladimir Safonov Oct 07 '19 at 12:51
  • [This answer](https://stackoverflow.com/a/25735202/1678362) might work for you, however when I [tested it](https://ideone.com/qu393Y) I found out the square brackets were not encoded, which differs from your expected result and seems to disagree with the standards. – Aaron Oct 10 '19 at 12:15

0 Answers0