Invalid URI with Chinese characters (Java)

2019-01-20 01:53发布

Having trouble setting up a URL connection with Chinese characters in the URL. It works with Latin characters:

String xstr = "维也纳恩斯特哈佩尔球场" ;
URI uri = new URI("http","ajax.googleapis.com","/ajax/services/language/detect","v=1.0&q="+xstr,null);   
URL url = uri.toURL(); 
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream() ;

The getInputStream() call results in:

java.lang.IllegalArgumentException: Invalid uri 'http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=???????????': Invalid query

4条回答
forever°为你锁心
2楼-- · 2019-01-20 02:33

The problem is caused by the fact that URI.toURL() doesn't percent-encode non-ASCII characters. Use the following instead:

URL url = new URL(uri.toASCIIString());  
查看更多
疯言疯语
3楼-- · 2019-01-20 02:33

axtavt's answer above saved me from insanity, thanks! Just one comment (I could not figure out how to comment below the answer:)

If you start with a URL, you need to encode quotes before you build the URI:

String s = "your_url?with=\"quotes\"";
URI su = new URI (s.replaceAll("\"", "%22");
URL ur = new URL( su.toASCIIString());
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-20 02:42

Per the URI RFC (see section 2.4), non-US-ASCII characters aren't valid in a URI. You must encode them.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-20 02:52

I think it is related to the "UTF-8" charset. Have a look at this topic to learn more and also this chinese in java

查看更多
登录 后发表回答