gpt4 book ai didi

java - 使用 Apache HttpClient 的 Https 和基本身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:25 25 4
gpt4 key购买 nike

我正在尝试为 WooCommerce API 实现 Java 客户端。我能够执行以下 curl 查询:

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 中呢?

最佳答案

我找到了至少对我有用的答案。这里仅进行了更改以将 SSL 支持添加到具有基本身份验证的初始 GET 请求:

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

关于java - 使用 Apache HttpClient 的 Https 和基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34296323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com