Invalid URI with Chinese characters (Java)

有些话、适合烂在心里 提交于 2019-11-28 12:44:41

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

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

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

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

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