使用Apache HttpClient进行Https和基本身份验证

时间:2015-12-15 18:02:17

标签: java ssl https woocommerce httpclient

我正在尝试为WooCommerce API实现Java客户端。 我可以做以下卷曲查询:

curl https://myserver/wc-api/v3/products -k -u  ck_mykey:cs_mysecret

并得到相关答案。 -k 代表不安全的连接。

我使用 Apache HttpClient 4.x 进行http连接。 谷歌搜索给了我一些 HttpClient 3.x 的例子。在HttpClient 4.x中 是3.x弃用的大部分内容。那么,任何人都可以共享一些Java + HttpClient实现 使用基本身份验证 https 客户端。

修改

好的,让我重新解释一下我的问题。我能流利地工作这样的代码 通过BasicAuthCache执行GET请求的基本身份验证:

public static void getWithBasicAuth(String host, String url, String username, String password)
        throws ClientProtocolException, IOException {

    HttpHost target = new HttpHost(host, 80, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(username, password));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpGet httpget = new HttpGet(url);

        System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
        for (int i = 0; i < 3; i++) {
            CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        }
    } finally {
        httpclient.close();
    }
}

但我应该如何将这种方法包装成 ssl

1 个答案:

答案 0 :(得分:1)

我发现答案对我来说至少有用。 这里仅进行了更改,以使用基本身份验证向初始GET请求添加SSL支持:

public static void getWithBasicAuthSSL(String host, String url, String username, String password)
        throws ClientProtocolException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            builder.build());



    HttpHost target = new HttpHost(host, 443, "https");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(username, password));

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).setSSLSocketFactory(sslsf).build();

    try {
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpGet httpget = new HttpGet(url);

        System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
        for (int i = 0; i < 3; i++) {
            CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        }
    } finally {
        httpclient.close();
    }
}