gpt4 book ai didi

java - Apache httpclient 未知主机问题

转载 作者:行者123 更新时间:2023-12-02 02:36:01 25 4
gpt4 key购买 nike

我正在使用以下 apache http 客户端片段,取自网站:

https://gist.github.com/Cyclenerd/41c737ee4b6ee4c767947af790d09e2c

这是发出简单获取请求的代码:

public final static void main(String[] args) throws Exception {
// Setup a Trust Strategy that allows all certificates.
// !!! DO NOT USE THIS IN PRODUCTION !!!
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();

// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
// Get URL
HttpGet httpget = new HttpGet("https://www.google.de");

System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(entity));
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

我不断收到以下异常:

执行请求 GET https://www.google.de HTTP/1.1线程“main”中的异常 java.net.UnknownHostException:www.google.de

最佳答案

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class StackOverFlowIssue {

public final static void main(String[] args) throws Exception {
// Setup a Trust Strategy that allows all certificates.
// !!! DO NOT USE THIS IN PRODUCTION !!!
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();

// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
// Get URL
HttpGet httpget = new HttpGet("https://www.google.de");

System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(entity));
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}

Pom依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

关于java - Apache httpclient 未知主机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201846/

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