【发布时间】:2018-09-09 17:30:04
【问题描述】:
我正在使用 HttpURLConnection 执行 POST 请求。
我读到 HttpsURLConnection 扩展了 HttpURLConnection 以支持 https 特定的功能,例如 SSL
如果HttpsURLConnection在HttpURLConnection(父类)中使用相同的方法,它会超级调用来自HttpURLConnection的方法。
URL url = new URL(callBackUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(setConnectTimeout);
connection.setReadTimeout(setConnectTimeout);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
os.write(data.toString());
os.flush();
os.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
while ((output = br.readLine()) != null) {
stringBuffer.append(output);
}
}
但是,当我使用 HTTPS 网址时,我不断收到以下错误;
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.8.0_181]
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964) ~[na:1.8.0_181]
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328) ~[na:1.8.0_181]
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322) ~[na:1.8.0_181]
问题,我如何处理 SSL 和非 SSL 连接,就像 PHP 中使用 CURL 时设置忽略 SSL 检查的情况一样
curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, true);
我在 Linux、Spring 框架和 TomCat Web 服务器上。
有人吗?
【问题讨论】:
标签: java spring spring-boot