RESTAssured - use .pfx certificate for https call

I am trying to make a HTTPs call using RESTAssured. I am using a .pfx certficate. But the call is ending in a handshake failure. Below is the code I am using.

 FileInputStream instream1=null; FileInputStream instream2=null; KeyStore trustStore=null; KeyStore keyStore=null; instream1 = new FileInputStream(new File(keystore)); keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(instream1, keystorepwd.toCharArray()); instream2 = new FileInputStream (new File(truststore)); trustStore = KeyStore.getInstance("jks"); trustStore.load(instream2, truststorepwd.toCharArray()); X509HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; org.apache.http.conn.ssl.SSLSocketFactory lSchemeSocketFactory=null; lSchemeSocketFactory = new org.apache.http.conn.ssl.SSLSocketFactory(keyStore, keystorepwd); lSchemeSocketFactory.setHostnameVerifier(hostnameVerifier); RestAssured.config = RestAssured.config().sslConfig(new SSLConfig().with().sslSocketFactory(lSchemeSocketFactory).and().allowAllHostnames()); response = RestAssured.given() .relaxedHTTPSValidation() .contentType("application/json") .header("Accept-Encoding","gzip,deflate") .body(\\body) .post() .then().log().all() .assertThat().statusCode(201) .assertThat().extract().response();

1 Answer

I was able to resolve the issue with a small modification.

FileInputStream instream1=null;
KeyStore keyStore=null;
instream1 = new FileInputStream(new File(keystore));
keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(instream1, keystorepwd.toCharArray());
X509HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
org.apache.http.conn.ssl.SSLSocketFactory lSchemeSocketFactory=null;
lSchemeSocketFactory = new org.apache.http.conn.ssl.SSLSocketFactory(keyStore, keystorepwd);
lSchemeSocketFactory.setHostnameVerifier(hostnameVerifier);
RestAssured.config = RestAssured.config().sslConfig(new SSLConfig().with().sslSocketFactory(lSchemeSocketFactory).and().allowAllHostnames());
response = RestAssured.given() .relaxedHTTPSValidation() .contentType("application/json") .header("Accept-Encoding","gzip,deflate") .body(\\body)
.post()
.then().log().all()
.assertThat().statusCode(201)
.assertThat().extract().response();
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like