我在使用java的post方法时遇到了一个问题。这是我的密码
final HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(formrequest.length()));
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.connect();
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.writeBytes(formrequest);
}
is = conn.getInputStream();
final BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
final String jsonText = readAll(rd);
final JSONObject json = new JSONObject(jsonText);
return json;
我尝试连接时遇到的例外是
21:14:51.661 [main] ERROR it.snam.ned.libs.dds.v2.TokenGetter - Unable to get Token for DDS Service
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
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.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at it.snam.ned.libs.dds.v2.TokenGetter.getDdsToken(TokenGetter.java:166)
at it.snam.ned.libs.dds.v2.TokenGetter.refresh(TokenGetter.java:116)
at it.snam.ned.libs.dds.v2.TokenGetter.getValidToken(TokenGetter.java:98)
at it.snam.ned.libs.dds.v2.AbstractDdsDaoImpl.getValidToken(AbstractDdsDaoImpl.java:30)
at it.snam.ned.libs.dds.v2.AbstractDdsDaoImpl.sendPostRequest(AbstractDdsDaoImpl.java:48)
at it.snam.ned.libs.dds.v2.DdsFolderDaoImpl.find(DdsFolderDaoImpl.java:68)
at Main.main(Main.java:17)
Exception in thread "main" it.snam.ned.libs.dds.errors.DdsException: java.net.SocketException: Connection reset
at it.snam.ned.libs.dds.v2.TokenGetter.getDdsToken(TokenGetter.java:183)
at it.snam.ned.libs.dds.v2.TokenGetter.refresh(TokenGetter.java:116)
at it.snam.ned.libs.dds.v2.TokenGetter.getValidToken(TokenGetter.java:98)
at it.snam.ned.libs.dds.v2.AbstractDdsDaoImpl.getValidToken(AbstractDdsDaoImpl.java:30)
at it.snam.ned.libs.dds.v2.AbstractDdsDaoImpl.sendPostRequest(AbstractDdsDaoImpl.java:48)
at it.snam.ned.libs.dds.v2.DdsFolderDaoImpl.find(DdsFolderDaoImpl.java:68)
at Main.main(Main.java:17)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
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.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at it.snam.ned.libs.dds.v2.TokenGetter.getDdsToken(TokenGetter.java:166)
... 6 more
但如果我试着和 Postman 联系,一切都会好起来的。这是 Postman 的要求
Postman 请求
这是我要访问的服务器的证书
ssl证书
这是我尝试过的,但没有奏效。
下载jce策略8并放入jre的安全路径
使用 RestTemplate
但也有同样的错误
更多信息:我同时使用了jdk1.8.0\u111和jdk1.8.0\u221
暂无答案!
目前还没有任何答案,快来回答吧!