HttpResponse无法使用不受信任的证书

时间:2012-02-01 16:45:45

标签: android https http-post

我正在尝试从链接 https://.... com:8455 获取内容 “服务器的证书不受信任” 。内容是:“测试”。

但是在 HttpResponse response = httpClient.execute(httpPost); 之后我得到了NullPointerException。 我正在使用下一个代码:

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();
    }

有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

NullPointerException来自于execute()返回null的事实。这意味着,没有回应。

至于原因,请搜索SO,有大量问题已经回答有关连接到自签名证书,或者根本没有检查到CA.