Мне нужно получить данные с сервера, говорящего по протоколу https, в мое приложение для Android.
Я получаю сертификат сервера динамически, как это:
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(null);
URLConnection uc = new URL("https://duckduckgo.com/").openConnection();
uc.connect();
Certificate certs[] = ((HttpsURLConnection) uc).getServerCertificates();
for (int i = 0; i < certs.length; i++) store.setCertificateEntry("cert_"+ i, certs[i]);
((HttpsURLConnection) uc).disconnect();
У меня есть пользовательский HttpClient:
public class MyHttpClient extends DefaultHttpClient {
final KeyStore store;
public MyHttpClient(KeyStore store) {
this.store = store;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
return new SSLSocketFactory(store);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
И получает данные с сервера следующим образом:
HttpEntity responseEntity;
URI url = new URI("https://duckduckgo.com/?q=android+java");
HttpGet httpGet = new HttpGet(url );
HttpClient client = new MyHttpClient(store);
httpGet.addHeader("Content-Type", "text/xml");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpResponse getResponse = client.execute(httpGet);
Все ли я отправляю и получаю в зашифрованном виде?
Я думал, может быть, мне нужно было предоставить серверу мой открытый ключ?