Your Java client needs the Bing's server public certificate to start the communication (SSLHandshake), you got the error because Bing's certificate is not in your default Java Keystore as a trusted certificate.
You can do the following:
- Get the Bing's certificate and import to your default Java Keystore for trusted certificates using keytool, your default Keystore named
cacerts
is usually located atJAVA_HOME\jre\lib\security\
keytool -importcert -file bingcertificate.cer -keystore cacerts -alias "bingAlias"
- Create a dummy
SSLSocketFactory
to get aSSLContext
which accepts any certificate and use it with your http connection object.
Factory example:
public class DummySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public DummySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
and then
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(new DummySSLSocketFactory());
- Create your own keystore, import the certificate and load it in your code to get a new factory
Keystore example:
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream(new File("keystoreCompletePath")), "passwdKeyStore");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sslFactory = ctx.getSocketFactory();
and then
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(sslFactory);