关于http:查询字符串参数的Java URL编码

Java URL encoding of query string parameters

假设我有一个网址

1
http://example.com/query?q=

我有一个用户输入的查询,例如:

random word £500 bank $

我希望结果是正确编码的URL:

1
http://example.com/query?q=random%20word%20%A3500%20bank%20%24

实现这一目标的最佳方法是什么?我尝试了URLEncoder并创建了uri/url对象,但没有一个是正确的。


(P)字母名称0应该是去。You only need to keep in mind to encide only the individual query string parameter name and/or value,not the entire URL,for sure not the query string parameter separator character EDOCX1 individual 1.Nor the parameter name-value separator character EDOCX1.(p)字母名称(P)Note that spaces in query parameters are represented by EDOCX1 commercial 3,not EDOCX1 commercial 4,which is legitimately valid.The EDOCX1 4 universal is usually to be used to represent spaces in uri itself(the part before the uri-query string separator character EDOCX1 individual 6),not in query string(the part after EDOCX1 individual 6).(p)(P)还注意到有两种不同的英文方法。One without charset argument and another with.一个没有标记的争论是忧郁的。从未使用过这一方法,也从未使用过这一方法。The Javado even explicitly recommends to use the UTF-8 encourage,as mandated by RfC3986 and W3C.(p)布尔奇1See also:

  • 要发帖,您需要先加入此论坛。


(P)我不会用字母名称0Besides being impully named(EDOCX1 simplication 0)has nothing to do with URLS),ineficient(it used a EDOCX1 indicated of builder and does a couple of other things that are slow)its also way too easy to screw it up.(p)(P)Instead I would use EDOCX1 pental 12 common or spring's EDOCX1 penal 13 or commons apache EDOCX1.The reason being you have to escape the query parameter s name(ie Balusc's answer EDOCX1 original 15)differently than the parameter value.(p)(P)The only down to the above(that I found out painfully)i s that URL's not a true subset of uri's.(p)(P)Sample Code:(p)字母名称(P)因为我只是把它与其他的答案联系起来,这是一个社区维基。Feel free to edit.(p)


(P)你需要先创造一个像你这样的人(p)字母名称(P)他们把Uri变成了Ascii String:(p)字母名称(P)Now your URL string is completely encued first we did simple URL encoming and then we turned it to ASCI string to make sure no character outside US-ASCI are remaining in string.这真是太棒了(p)


(P)Guava 15 has now added a set of straightforward URL escapers.(p)


(P)Apache http://components library provides a neat option for building and encourage query params-(p)(P)With httpcomponents 4.x use-乌兰(p)(P)For httpclient 3.x use-缩小(p)


(P)在这里,你可以使用一种方法在你的法典中转换一个URL String and Map of Parameters to a valid encoed URL string containing the query parameters.(p)字母名称


(P)Use the following standard Java solution(passes around 100 of the testcases provided by web platform tests):(p)(P)0.如果URL是永久缩小。Replace'+'enceded spaces with'%20'enceded spaces.(p)(P)1.将URL转化为结构党。使用EDOCX1为它。(p)(P)2.Each Structural Part Properly!(p)(P)3.使用EDOCX1 19字体到上帝的名字。(p)(P)4.使用EDOCX1 20发音到Percent-Encode,NFC enceded Unicode(Better Would be NFCC!)For more info see:how to encode properly this URL(p)字母名称(P)Prints(p)字母名称(P)这里有些例子表明,这也将是一项工作。(p)字母名称


(P)In my case I just need to pass the whole URL and encode only the value of each parameters.I did't find a common code to do that so…!So I created this small method to do the job:(p)字母名称(P)IT uses org.apache.commons.lang3.stringutils(p)


(P)In Android I would use this code:(p)字母名称(P)伊多克X1字体16是伊多克X1字体17(p)


  • Use this:Urlencoder.encode(query,standard charsets.utf---U8.displayename();Or this:Urlencoder.encode(query,"UTF-8");
  • (P)你可以使用民间法典。(p)字母名称