[英]SSL connection with certificate
我想將以下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 =“ server.crt”,cert_reqs = ssl.CERT_REQUIRED)
嘗試-Djavax.net.ssl.trustStore=/path/to/keystore
,其中“ / path / to / keystore”是備用密鑰庫的絕對文件路徑。
這必須是用於運行代碼/項目的VM選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.