HttpResponse 不适用于不受信任的证书

发布于 2025-01-01 06:06:45 字数 1313 浏览 0 评论 0原文

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

有人可以帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2025-01-08 06:06:45

NullPointerException 来自以下事实:execute() 返回 null。也就是说,没有任何反应。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文