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
使用帶有多個參數(shù)的構(gòu)造函數(shù),如:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/first book.pdf",
null);
URL url = uri.toURL();
//or String request = uri.toString();
(URI的單參數(shù)構(gòu)造函數(shù)不轉(zhuǎn)義非法字符)
編輯:添加完全限定類名,以避免與其他URI類(如apaches httpclient)混淆,
EDIT 2:
只有非法字符通過上述代碼轉(zhuǎn)義 – 它不轉(zhuǎn)義非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的查詢的網(wǎng)址,請使用構(gòu)造函數(shù)的5參數(shù)版本:
URI uri = new URI(
"http",
"www.google.com",
"/ig/api",
"weather=S?o Paulo",
null);
String request = uri.toASCIIString();
總結(jié)
以上是生活随笔為你收集整理的java http url 编码_Java中的HTTP URL地址编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux6.4 dns 主从,dns正
- 下一篇: 哈维玛德学院 计算机,哈维玛德学院优势多