Apache httpclient未知主机问题

时间:2019-07-25 12:20:58

标签: java apache-httpclient-4.x

我正在使用以下来自网站的apache http客户端代码段:

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

以下是发出简单的get请求的代码:

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 线程“主” java.net.UnknownHostException中的异常:www.google.de

3 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

该代码可用于以下导入:

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

库pom.xml的次要版本更改:

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.9</version>
    </dependency>

可能与进口不当有关。

答案 2 :(得分:0)

这个答案可能是一个话题,但是虽然apache Http客户端是非常流行和经过测试的库,但似乎只是发送一个简单的GET请求的大量代码。有一个非常简单且鲜为人知的Http客户端,它可能无法处理一些复杂的问题,但是对于像这样的简单内容,使用起来绝对更友好。下面是将GET请求发送到“ https://www.google.de”链接的代码。

private static void testHttpClient() {
    HttpClient client = new HttpClient();
    client.setContentType("text/html; charset=utf-8");
    String content = null;
    try {
        content = client.sendHttpRequest("https://www.google.de", HttpMethod.GET);
    } catch (IOException e) {
        content = TextUtils.getStacktrace(e, false);
    }
    System.out.println(content);
}

如果您有兴趣,该库称为MgntUtils(由我编写),您可以将其命名为Maven artifactsGit(包括源代码和javadoc)。这是Javadoc的链接,这是link to an article about the library的Maven工件:

<dependency>
  <groupId>com.github.michaelgantman</groupId>
  <artifactId>MgntUtils</artifactId>
  <version>1.5.0.7</version>
</dependency>