java http url 编码_Java中的HTTP URL地址编码
java.net.URI類可以幫助;在URL的文檔中找到
Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI
使用帶有多個參數的構造函數,如:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/first book.pdf",
null);
URL url = uri.toURL();
//or String request = uri.toString();
(URI的單參數構造函數不轉義非法字符)
編輯:添加完全限定類名,以避免與其他URI類(如apaches httpclient)混淆,
EDIT 2:
只有非法字符通過上述代碼轉義 – 它不轉義非ASCII字符(參見fatih的注釋)。
toASCIIString方法可用于獲取僅包含US-ASCII字符的字符串:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/é",
null);
String request = uri.toASCIIString();
編輯3:
對于包含諸如http://www.google.com/ig/api?weather=S?oPaulo的查詢的網址,請使用構造函數的5參數版本:
URI uri = new URI(
"http",
"www.google.com",
"/ig/api",
"weather=S?o Paulo",
null);
String request = uri.toASCIIString();
總結
以上是生活随笔為你收集整理的java http url 编码_Java中的HTTP URL地址编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux6.4 dns 主从,dns正
- 下一篇: java美元兑换,(Java实现) 美元