简体   繁体   English

Apache httpclient未知主机问题

[英]Apache httpclient unknown host issue

I am using the following apache http client snippet, taken from the website: 我正在使用以下从网站获取的apache http客户端代码段:

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

Here is the code making a simple get request: 这是发出简单的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();
    }
 }

I keep getting the following exception : 我不断收到以下异常:

Executing request GET https://www.google.de HTTP/1.1 Exception in thread "main" java.net.UnknownHostException: www.google.de 执行请求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 Pom依赖

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

The code works with the following imports: 该代码适用于以下导入:

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;

Minor version change of the library, pom.xml: 库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>

Problably there are some problems related to incorrent imports. 可能有一些与进口不当有关的问题。

This answer may be a bit of topic, but while apache Http client is very popular and tested library, it seems like a lot of code just to send a simple GET request. 这个答案可能是一个话题,但是虽然apache Http客户端是非常流行和经过测试的库,但是看起来好像很多代码只是发送一个简单的GET请求。 There is a very simple and by far less known Http client that may not handle some complex issues, but for simple stuff like this is definitely more friendly to use. 有一个非常简单且鲜为人知的Http客户端,它可能无法处理一些复杂的问题,但是对于像这样的简单内容,使用起来绝对更友好。 Below is the code that sends a GET request to " https://www.google.de " link. 以下是将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);
}

If you are interested, the library is called MgntUtils (written by me) and you can get it as Maven artifacts or Git (including source code and javadoc). 如果您有兴趣,该库称为MgntUtils(由我编写),您可以将其作为Maven工件Git (包括源代码和javadoc)获得。 Here is Link to Javadoc and Here is a link to an article about the library Here is maven artifact: 这是Javadoc链接 ,这是有关该库的文章链接。这是Maven工件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM