查询字符串参数的Java URL编码

本文翻译自:Java URL encoding of query string parameters

Say I have a URL 说我有一个网址

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

and I have a query entered by the user such as: 并且我有一个用户输入的查询,例如:

random word £500 bank $ 随机词£500银行$

I want the result to be a properly encoded URL: 我希望结果是正确编码的URL:

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

What's the best way to achieve this? 实现此目标的最佳方法是什么? I tried URLEncoder and creating URI/URL objects but none of them come out quite right. 我尝试了URLEncoder并创建URI / URL对象,但是没有一个是正确的。


#1楼

参考:https://stackoom.com/question/jFwQ/查询字符串参数的Java-URL编码


#2楼

URLEncoder should be the way to go. URLEncoder应该是要走的路。 You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character = . 您只需要记住仅对单个查询字符串参数名称和/或值进行编码,而不对整个URL进行编码,请确保对查询字符串参数分隔符&和参数名称-值分隔符=不进行编码。

String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

Note that spaces in query parameters are represented by + , not %20 , which is legitimately valid. 请注意,查询参数中的空格由+表示,而不是%20 ,这是合法有效的。 The %20 is usually to be used to represent spaces in URI itself (the part before the URI-query string separator character ? ), not in query string (the part after ? ). %20通常用于表示URI本身(URI查询字符串分隔符?之前的部分)而不是查询字符串( ?后面的部分)中的空格。

Also note that there are two encode() methods. 还要注意,有两种encode()方法。 One without charset argument and another with. 一个不带charset参数,另一个不带charset参数。 The one without charset argument is deprecated. 不带charset参数的参数已弃用。 Never use it and always specify the charset argument. 从不使用它,并且始终指定charset参数。 The javadoc even explicitly recommends to use the UTF-8 encoding, as mandated by RFC3986 and W3C . Javadoc甚至明确建议使用RFC3986W3C要求的UTF-8编码。

All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. 所有其他字符都是不安全的,并且首先使用某种编码方案转换为一个或多个字节。 Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. 然后,每个字节由3个字符的字符串“%xy”表示,其中xy是该字节的两位十六进制表示形式。 The recommended encoding scheme to use is UTF-8 . 推荐使用的编码方案是UTF-8 However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used. 但是,出于兼容性原因,如果未指定编码,则使用平台的默认编码。

See also: 也可以看看:


#3楼

I would not use URLEncoder . 我不会使用URLEncoder Besides being incorrectly named ( URLEncoder has nothing to do with URLs), inefficient (it uses a StringBuffer instead of Builder and does a couple of other things that are slow) Its also way too easy to screw it up. 除了被错误地命名( URLEncoder与URL无关)之外,效率低下(它使用StringBuffer代替Builder,并且执行其他一些很慢的操作)它也很容易将其弄乱。

Instead I would use URIBuilder or Spring's org.springframework.web.util.UriUtils.encodeQuery or Commons Apache HttpClient . 相反,我将使用URIBuilderSpring的org.springframework.web.util.UriUtils.encodeQuery或Commons Apache HttpClient The reason being you have to escape the query parameters name (ie BalusC's answer q ) differently than the parameter value. 原因是您必须以与参数值不同的方式转义查询参数名称(即BalusC的答案q )。

The only downside to the above (that I found out painfully) is that URL's are not a true subset of URI's . 上面的唯一缺点(我很痛苦地发现)是URL并不是URI的真正子集

Sample code: 样例代码:

import org.apache.http.client.utils.URIBuilder;

URIBuilder ub = new URIBuilder("http://example.com/query");
ub.addParameter("q", "random word £500 bank \$");
String url = ub.toString();

// Result: http://example.com/query?q=random+word+%C2%A3500+bank+%24

Since I'm just linking to other answers I marked this as a community wiki. 由于我只是链接到其他答案,因此将其标记为社区Wiki。 Feel free to edit. 随时编辑。


#4楼

Guava 15现在添加了一组简单的URL逸出器


#5楼

You need to first create a URI like: 您首先需要创建一个URI,例如:

    String urlStr = "http://www.example.com/CEREC® Materials & Accessories/IPS Empress® CAD.pdf"
    URL url= new URL(urlStr);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());

Then convert that Uri to ASCII string: 然后将该Uri转换为ASCII字符串:

    urlStr=uri.toASCIIString();

Now your url string is completely encoded first we did simple url encoding and then we converted it to ASCII String to make sure no character outside US-ASCII are remaining in string. 现在,您的url字符串已完全编码,我们先进行了简单的url编码,然后将其转换为ASCII字符串,以确保字符串中没有剩余US-ASCII之外的字符。 This is exactly how browsers do. 这正是浏览器的工作方式。


#6楼

In android I would use this code: 在android中,我将使用以下代码:

Uri myUI = Uri.parse ("http://example.com/query").buildUpon().appendQueryParameter("q","random word A3500 bank 24").build();

Where Uri is a android.net.Uri 其中Uriandroid.net.Uri

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值