查詢字符串參數的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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章