I am trying get content from the link https://….com:8455 where “Server’s certificate is not trusted”. The content is: “test”.
But I get NullPointerException after HttpResponse response = httpClient.execute(httpPost);.
I am using the next code:
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 8455));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
final String url = "https://....com:8455";
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.v("data: ", HttpHelper.requestStr(response));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Can anybody help me?
The NullPointerException comes from the fact, that execute() returned null. That means, that there was no response.
As to the causes of this, please search SO, there are PLENTY of questions answered about connecting to a self-signed certificate, or not checking to a CA at all.