RESTAssured - 使用.pfx证书进行https呼叫

时间:2017-02-14 20:28:17

标签: https rest-assured

我正在尝试使用RESTAssured进行HTTPs调用。我正在使用.pfx证书。但是这个电话以握手失败告终。以下是我正在使用的代码。

    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 个答案:

答案 0 :(得分:0)

我能够通过一些小修改来解决问题。

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();