Invalid URI with Chinese characters (Java)

Deadly 提交于 2019-11-27 07:14:59

问题


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

回答1:


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());  



回答2:


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());



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/4831301/invalid-uri-with-chinese-characters-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!