如何在Java中发出HTTPS发布请求? [英] How to make HTTPS post request in Java?

查看:373
本文介绍了如何在Java中发出HTTPS发布请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出HTTPS发布请求.

I'm trying to make a HTTPS post request.

String url = "https://myhttpsurl.com"
URL myurl = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
con.setRequestMethod("POST");
String query = "confirmation_number=" + URLEncoder.encode(confNumber, "UTF-8");
con.setRequestProperty("Content-length",String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);

DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);

output.close();
System.out.println("Resp Code:"+con.getResponseCode());

我遇到以下错误:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法执行 在以下位置找到到所需目标的有效认证路径 sun.security.ssl.Alerts.getSSLException(Alerts.java:192)在 sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)位于 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446) 在 sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209) 在sun.security.ssl.Handshaker.processLoop(Handshaker.java:913)处 sun.security.ssl.Handshaker.process_record(Handshaker.java:849)在 sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)在 sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343) 在 sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 在 sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:913) at sun.security.ssl.Handshaker.process_record(Handshaker.java:849) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)

请提出导致错误的原因

推荐答案

Java无法在https服务器上验证SSL证书的有效性.它可以是自签名的,也可以由Java运行时不知道的证书颁发机构签名.

Java is not able to verify the validity of the SSL certificate on the https server. It is either self-signed or signed by a certificate authority not known to the Java runtime.

您的选择是通过将证书添加到信任库(证书)来手动信任证书,或者对证书进行签名.我不建议您完全禁用SSL证书验证,因为您可能会承受意料之外的后果.

Your options are to manually trust the certificate by adding it to the truststore (cacerts), or to have your certificate signed. I don't recommend disabling the SSL certificate validation completely, as you might open yourself to unexpected consequences.

这篇关于如何在Java中发出HTTPS发布请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆