我尝试测试一种“奇怪”的GET请求,其中我必须提供基本身份验证和客户端证书。
我尝试使用Postman Chrome进行检查,但我不知道如何将chrome个人证书与我的请求链接起来。
我看到了这个讨论:https://github.com/a85/POSTMan-Chrome-Extension/issues/482,但它是关于MAC密钥库的,我无法将其转换到W7 / Chrome。
这是我的Java代码设置,应该可以像Postman那样完成同样的工作,以帮助您了解我想要Postman做什么。我们使用该帖子来编写它。
InputStream is = context.getResources().getAssets().open("CertificateFile.p12");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
BufferedInputStream bis = new BufferedInputStream(is);
String password ="xxxxx";
keyStore.load(bis, password.toCharArray()); // password is the PKCS#12 password. If there is no password, just pass null
// Init SSL Context
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(keyStore, password.toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, null, null);
HttpsURLConnection urlConnection = null;
String strURL = "theUrlITryToHit";
url = new URL(strURL);
urlConnection = (HttpsURLConnection) url.openConnection();
if(urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection)urlConnection)
.setSSLSocketFactory(sslContext.getSocketFactory());
}
urlConnection.setRequestMethod("GET");
String basicAuth = "Basic " + Base64.encodeToString("pseudo:password".getBytes(), Base64.NO_WRAP);
urlConnection.setRequestProperty ("Authorization", basicAuth);