0

我正在下载网页内容,但地址有特殊字符。例如,它包含adiós. myUrl我正在使用下面的代码,但它不成功。你有什么建议吗?谢谢

String myUrl="http://www.somethingxxxxx.com/adiós";   
URLConnection yc = new URL(myUrl).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
                                                yc.getInputStream()));
4

2 回答 2

2

使用URLEncoder类而不是 java.netURL

于 2012-09-15T05:20:24.467 回答
1

您只需要在使用 URL 之前对其进行编码。为此,您可以尝试以下操作:

try {
    String base = "http://www.somethingxxxxx.com/";
    String toEncode = "adiós";
    String myEncodedUrl = base + URLEncoder.encode( toEncode, "UTF-8" );
    URLConnection yc = new URL(myEncodedUrl).openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                            yc.getInputStream()));
} catch ( UnsupportedEncodingException exc ) {
   exc.printStackTrace();
} catch ( IOException exc ) {
   exc.printStackTrace();
}
于 2012-09-15T05:41:25.650 回答