Java
Chilkat2 Java
Use HTTPS Client Certificate from .cer and .key Files
See more HTTP Examples
Demonstrates how to load a cert + private key from .cer and .key (base64) files and use it for mutual TLS authentication (client-side certificate).Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
Cert cert = new Cert();
PrivateKey privKey = new PrivateKey();
// Load any type of certificate (.cer, .p7b, .pem, etc.) by calling LoadFromFile.
try {
cert.loadFromFile("qa_data/certs/sample_cert_a.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Load the private key.
BinData bd = new BinData();
bd.loadFile("qa_data/certs/sample_key_a.key");
try {
privKey.loadAnyFormat(bd, "privateKeyPasswordIfNecessary");
// Associate the private key with the cert.
cert.setPrivateKey(privKey);
// Set the certificate to be used for mutual TLS authentication
// (i.e. sets the client-side certificate for two-way TLS authentication)
http.setSslClientCert(cert);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// At this point, the HTTP object instance is setup with the client-side cert, and any SSL/TLS
// connection will automatically use it if the server demands a client-side cert.
}
}