如何使用Rest-Assured java中的证书进行HTTPS GET调用 [英] How to make HTTPS GET call with certificate in Rest-Assured java
本文介绍了如何使用Rest-Assured java中的证书进行HTTPS GET调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Rest-Assured 在Java中执行GET调用,以便在需要证书。我有证书 .pem
格式。在PEM文件中有证书和私钥。
How can I make a GET call using Rest-Assured in java to a endpoint which requires certificate. I have certificate as .pem
format. In PEM file there is certificate and private key.
推荐答案
使用下面的代码 -
Got it working with following code -
KeyStore keyStore = null;
SSLConfig config = null;
try {
keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(
new FileInputStream("certs/client_cert_and_private.p12"),
password.toCharArray());
} catch (Exception ex) {
System.out.println("Error while loading keystore >>>>>>>>>");
ex.printStackTrace();
}
if (keyStore != null) {
org.apache.http.conn.ssl.SSLSocketFactory clientAuthFactory = new org.apache.http.conn.ssl.SSLSocketFactory(keyStore, password);
// set the config in rest assured
config = new SSLConfig().with().sslSocketFactory(clientAuthFactory).and().allowAllHostnames();
RestAssured.config = RestAssured.config().sslConfig(config);
RestAssured.given().when().get("/path").then();
这篇关于如何使用Rest-Assured java中的证书进行HTTPS GET调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文