InputStream in = address.openStream();
URL url = new URL("://www.mydomain.com/?param1=NÃO¶m2=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ÃO特殊字符Ã已被转义
如何在不损失字符集值的情况下实现这一目标?
最佳答案
我相信您想使用URLEncoder.encode(String, String)
对参数进行编码,例如
try {
String value = URLEncoder.encode("NÃO", "utf-8");
String url = "://www.mydomain.com/?param1=" + value + "¶m2="
+ value;
System.out.println(url);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
输出是
://www.mydomain.com/?param1=N%C3%83O¶m2=N%C3%83O