在调用HTTPS休息服务时,我需要您的帮助。
某些背景, 我需要调用以HTTPS托管的rest服务。
我获得了证书,并且设法得到了邮递员的答复。 非常重要,只有当我在Postman上将证书声明为“ ca证书”时,才可以连接,如果我将其声明为“ 客户端证书”,则我无法连接。>
现在我想通过java调用服务,所以我可以通过keytool生成JKS:
C:\ WINDOWS \ system32> keytool-导入-trustcacerts-别名Alias PublicKeyFromCEF-文件service.cer -keystore ABCD.jks
我建立了一个客户
System.setProperty("javax.net.ssl.trustStore", "c:/ABCD.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
char[] passphrase = "123456".toCharArray(); //password
KeyStore keystore = KeyStore.getInstance("JKS");
//KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream("c:/ABCD.jks"), passphrase); //path
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
SSLContext context = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = tmf.getTrustManagers();
context.init(null, trustManagers, null);
SSLSocketFactory sf = context.getSocketFactory();
URL url = new URL("https://services/api/v1/doctors?docId=5555");
HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
httpsCon.setSSLSocketFactory(sf);
httpsCon.setRequestMethod("GET");
System.out.println("Response Message is " + httpsCon.getResponseMessage());
当我调用服务时,出现错误: java.net.SocketException:方法java.net.SocketInputStream#socketRead
我怀疑我没有正确生成JKS
以下是调试时的对象状态,希望它能为您带来启发:
在方法sun.security.ssl.SSLSocketImpl#performInitialHandshake
中:
this.input.r.helloVersion = LTSv1
this.input.r.getHandshakeHash() : mds,sha are null, data is populated.
在方法中:sun.security.ssl.InputRecord#read
this.exlen =0
然后我在调用java.net.SocketInputStream#tRead
时在n = socketRead(fd, b, off, length, timeout);
上遇到异常