【发布时间】:2020-05-02 12:00:55
【问题描述】:
我有一个我喜欢通过 java 应用程序解析的 URL。 这些 url 可以有字符,不能被调用:
url.openStream()
示例:
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=langlinks&titles=2019–20_coronavirus_pandemic&redirects=&lllimit=400
其中有一个字符(2019–20_coronavirus_pandemic),我必须对其进行编码。响应。我想对完整的 URL 进行编码,因为它可能包含其他特殊字符。
我这样做如下,这对我不起作用:
String urlEncoded = URLEncoder.encode(wikiID, StandardCharsets.UTF_8.toString());
String sURL = "https://en.wikipedia.org" + "/w/api.php?format=json&action=query&prop=langlinks&titles=" + urlEncoded + "&redirects=&lllimit=400";
URL url = new URL(sURL);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
URLEncoder.encode 将 2019–20 编码为 2019%3F20,这是不正确的,分别。无法调用。 正确的编码是:2019%E2%80%9320
如何正确编码url?
【问题讨论】: