本帖最后由 包宇杰88 于 2018-2-12 17:13 编辑
同样访问这个网站:https://chun.woaigames.xyz/find.php?name=1223
浏览器显示:yes|1518423733|33075326224|长长记性|CONSOLE
而JAVA却返回:yes|1518423733|33075326224|闀块暱璁版??CONSOLE
这是JAVA的连接代码,为什么会乱码呢?我那里也设置成UTF-8了啊。求大神帮忙
浏览器显示:yes|1518423733|33075326224|长长记性|CONSOLE
而JAVA却返回:yes|1518423733|33075326224|闀块暱璁版??CONSOLE
String result = "";
BufferedReader in = null;
try {
String a = to.replace(" ", "+");
String urlNameString = "https://chun.woaigames.xyz/find.php?name=1223";
getLogger().info(urlNameString);
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Referer", "https://www.baidu.com/");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
/*
* Map<String, List<String>> map = connection.getHeaderFields(); //
* 遍历所有的响应头字段 for (String key : map.keySet()) {
* System.out.println(key + "--->" + map.get(key)); }
*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
这是JAVA的连接代码,为什么会乱码呢?我那里也设置成UTF-8了啊。求大神帮忙
同样访问这个网站:https://chun.woaigames.xyz/find.php?name=1223
浏览器显示:yes|1518423733|33075326224|长长记性|CONSOLE
而JAVA却返回:yes|1518423733|33075326224|闀块暱璁版??CONSOLE
这是JAVA的连接代码,为什么会乱码呢?我那里也设置成UTF-8了啊。求大神帮忙
2021.12 数据,可能有更多内容
同样访问这个网站:https://chun.woaigames.xyz/find.php?name=1223浏览器显示:yes|1518423733|33075326224|长长记性|CONSOLE
而JAVA却返回:yes|1518423733|33075326224|闀块暱璁版??CONSOLE
String result = "";
BufferedReader in = null;
try {
String a = to.replace(" ", "+");
String urlNameString = "https://chun.woaigames.xyz/find.php?name=1223";
getLogger().info(urlNameString);
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Referer", "https://www.baidu.com/");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
/*
* Map<String, List<String>> map = connection.getHeaderFields(); //
* 遍历所有的响应头字段 for (String key : map.keySet()) {
* System.out.println(key + "--->" + map.get(key)); }
*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
这是JAVA的连接代码,为什么会乱码呢?我那里也设置成UTF-8了啊。求大神帮忙
你需要转换编码,就不给出示例了。
光设置显示编码应该没用……你可以尝试先转换再输出
langyo_v3 发表于 2018-2-12 17:17
光设置显示编码应该没用……你可以尝试先转换再输出
貌似直接转换会丢失最后一个字,看来我只好用unicode保存读取时再转换了
dsadsadsadas
转码啊。。 这肯定不用说的