我想将以下Python代码复制到Java中的等效代码中:
import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s,
ca_certs="server.crt",
cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('localhost', 10023))
ssl_sock.write("boo!")
if False:
data = ssl_sock.read()
ssl_sock.close()
我想出了以下代码(这不起作用),但我还有两个问题:
abstract class Connection extends AsyncTask<String, Void, String> {
boolean Connect(){
try
{
SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket mySSLSocket=(SSLSocket) factory.createSocket("localhost", 10023);
DataOutputStream myOutPutStream = new DataOutputStream(mySSLSocket.getOutputStream());
myOutPutStream.writeBytes("Hello World!");
myOutPutStream.close();
mySSLSocket.close();
return true;
}
catch(Exception e)
{
return false;
}
}
}
通过执行代码,我得到以下Java错误:
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
我在哪里可以将证书保存在Android应用程序项目(Android Studio)中?
以下代码在Java中的等价物是什么?
ssl_sock = ssl.wrap_socket(s, ca_certs =&#34; server.crt这&#34 ;, cert_reqs = ssl.CERT_REQUIRED)
答案 0 :(得分:0)
试试-Djavax.net.ssl.trustStore=/path/to/keystore
,其中&#39; / path / to / keystore&#39;是备用密钥库的绝对文件路径。
这需要是运行代码/项目的VM选项。