如何在阅读URL时逃避NÃO

时间:2014-11-22 03:59:58

标签: java utf-8

InputStream in = address.openStream();
URL url = new URL("://www.mydomain.com/?param1=NÃO&param2=NÃO");   
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
    result.append(line);
}
System.out.println(result.toString());

但是当我试图将结果放入StringBuilder时,NÃOpecial字符Ã正在逃脱 如何在不丢失char值的情况下使用它?

1 个答案:

答案 0 :(得分:0)

我相信您希望使用URLEncoder.encode(String, String)对参数进行编码,例如

try {
    String value = URLEncoder.encode("NÃO", "utf-8");
    String url = "://www.mydomain.com/?param1=" + value + "&param2="
            + value;
    System.out.println(url);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

输出

://www.mydomain.com/?param1=N%C3%83O&param2=N%C3%83O