咋我设置了 utf-8 还是会乱码嘞
浏览器/postman 请求就不会、链接如下:
https://whois.pconline.com.cn/ip.jsp?ip=223.106.133.72

Java 代码如下:
public static JSONArray getHttpJsonBean(String url) {
StringBuffer json = new StringBuffer();
try {
// 通过 url 获得连接
URL u = new URL(url);
URLConnection yc = u.openConnection();
// 读取返回的数据
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
// 获得 JSONObject
JSONArray objects = JSON.parseArray(json.toString());
return objects;
}
你这个是IDE的问题吧,你用postMan去模拟请求,看看结果。
如果是你IDE的问题,修改一下IDE的编码。
破案了是不是
// new InputStreamReader(yc.getInputStream(),"gbk")); 改掉这个就ok了 public static String httpGetString(String urlPath) throws IOException { String result = ""; try { // 通过 url 获得连接 URL u = new URL(urlPath); URLConnection yc = (HttpURLConnection)u.openConnection(); yc.setDoInput(true); //设置输入流采用字节流 yc.setDoOutput(true); //设置输出流采用字节流 yc.setUseCaches(false); //设置缓存 yc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); yc.setRequestProperty("Charset", "utf-8"); yc.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(yc.getInputStream(),"gbk")); String line = ""; while(null != (line=br.readLine())){ result += line; } System.out.println("接到的数据: " + result); br.close(); } catch (Exception e) { e.printStackTrace(); } return result; }应该是你输出文本的编码和你IDE的编码不一致导致的,之所以你改成 GBK 编码就可以了,是因为你输出文本的编码和你IDE的编码一致了
卧槽,这种原始的请求做法,我好久没看到了