401未经授权使用SSL和HttpClient库

时间:2014-04-21 15:48:10

标签: java apache ssl httpclient

当我尝试使用ssl

登录应用时
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy())
            .build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1"},
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        HttpPost httppost = new HttpPost("https://otherwebapp.my.com/login");
        BasicCredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(provider).build();
        CloseableHttpResponse response = httpclient.execute(httppost);

我获得401 Unauthorized

当我在Chrome Advanced Rest Client视图中通过POST登录时 一切顺利。

我通过Wireshark监控流量看待差异

发现我的代码和chrome Advanced Rest Client视图之间没有区别

如果有人有想法,请帮助我

0 个答案:

没有答案