【问题标题】:HttpResponse is not working with untrusted certificateHttpResponse 不适用于不受信任的证书
【发布时间】:2012-02-24 08:40:37
【问题描述】:

我正在尝试从 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();
    }

谁能帮帮我?

【问题讨论】:

    标签: android https http-post


    【解决方案1】:

    NullPointerException 来自于 execute() 返回 null 的事实。也就是说,没有回应。

    至于造成这种情况的原因,请搜索 SO,关于连接到自签名证书或根本不检查 CA 的问题有很多答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2020-04-13
      • 2019-05-20
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多