What is the reason for the difference between these two urlencodings of url

瘦欲@ 提交于 2019-12-10 17:21:29

问题


Some queries encoded as UTF-8 that I send to a server are not returning the expect results.

i.e

http://direct.jthinkws.com?type=release&query=artist%3A%28Dinosaur%7E0.7+AND+Jr.%29++AND++%28%2Btrack%3A%22Forget+The+Swan%22+%2Btrack%3A%22Just+Like+Heaven%22+%29++AND+tracks%3A%5B2+TO+100%5D++AND+src%3A1&limit=20&offset=0

is only returning two results (results are returned as Xml) in my application and only 2 results if I put directly into Firefox browser

However if I put the non-encoded url value into Firefox

http://direct.jthinkws.com?type=release&query=artist:(Dinosaur~0.7 AND Jr.) AND (+track:"Forget The Swan" +track:"Just Like Heaven" ) AND tracks:[2 TO 100] AND src:1&limit=20&offset=0

it returns 44 files

and from my server I can see I get the following request which I assume must be firefox encoding the data

http://direct.jthinkws.com?type=release&query=artist:(Dinosaur~0.7%20AND%20Jr.)%20%20AND%20%20(+track:"Forget%20The%20Swan"%20+track:"Just%20Like%20Heaven"%20)%20%20AND%20tracks:[2%20TO%20100]%20%20AND%20src:1&limit=20&offset=0

as you can see it is encoding it slightly differently - spaces are being converted to '%20' not '+' and '(' and ')' are not converted.

I dont understand the difference and why one works and one doesn't, also why the one that doesnt work does return some results just not as many.

(Also I tried encoding as ISO-8859-1 instead of UTf-8) and that completely failed the server couldnt decode it so Im sure UTf8 is the correct encoding.

My code is written in Java and its encodes the value of the query using UREncoder, i.e

String query = URLEncoder.encode(queryValue.toString(), StandardCharsets.UTF_8.name());

回答1:


I had this the wrong way round my code was actually working fine it should only return two results.

The problem was that Firefox was unable to correctly encode the +'s assuming them to be already encoded spaces. So you cant always rely on Firefox to correctly encode a unencoded url that you paste into it.

This problem is not specific to Firefox, it maybe that no browser could manage to encode this properly. So if you are using a url that includes '+'s be careful.



来源:https://stackoverflow.com/questions/33476604/what-is-the-reason-for-the-difference-between-these-two-urlencodings-of-url

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