简体   繁体   中英

Connect to ssl server using java client

I am given a certificate by my api provider which are .crt file and .p12 file.

I generated ca-cert.pem file from .crt file and client-cert.pem and client-key.pem file from .p12 file using openssl as required by api.

I am also provided with username password for basic authentication by my api provider.

My question is how can I connect to server url https (rest) using these certificate and credentials using Java.

You basically have to create an SSLSocketFactory see here for an example . Once you've created the SSLSocketFactory you can set it when you create a Connection from the URL like so.

SSLSocketFactory sf = ... see that example
URL url = new URL("https://google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(sf);
... Now do stuff with that connection like GET or POST

Also fyi, you are going to want to convert your certificates into the JKS format that Java prefers in order to easily load them into the SSLSocketFactory. You can use a program called keytool that comes with java which can help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM