尝试使用POST方法从API获取数据。并返回
线程中的异常" main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 找到所请求目标的有效证书路径
我已经考虑过修复它,但所有答案都非常模糊,它与Installcert
有关,但没有在线有指示。我无法弄明白,因为我在编程方面相对较新。任何帮助将非常感谢
public static void main(String[] args) throws Exception {
URL url = new URL("https://probasketballapi.com/stats/players");
Map<String,Object> params = new LinkedHashMap<>();
params.put("api_key", "8Z9zCply76uBnEDF3itvshqJOPd4Rjoe");
params.put("first_name", "Kevin");
params.put("last_name", "Durant");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);