代理后面的UnknownHostException

时间:2019-05-02 15:38:17

标签: java networking proxy

我有一个Java程序,该程序连接到网站以从中检索一些XML。这在我的计算机以及公司以外的其他计算机上都可以正常工作。我们的一位客户现在无法连接到该网站。我发现,它们在代理后面。现在,我已经找到了需要使用的设置,并且在我的测试程序中(部分)可以使用。

在下面的代码中,downloadFile()调用可以正常工作,并且可以毫无问题地下载文件。 contactHost()在我们的客户端计算机上失败,出现UnknownHostException:

java.net.UnknownHostException: No such host is known (api.myserver.de)
    at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:925)
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1505)
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:844)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1495)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1354)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1288)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)

背景:Windows 10计算机,我们的程序随附内部OpenJDK,版本为“ 10.0.2”,2018-07-17。该程序从以下定义-Djdk.http.auth.tunneling.disabledSchemes="" -Djava.net.preferIPv4Stack=true开始,以便仅使用IP4并为代理启用BasicAuthentification。通过这些设置,可以下载文件,但是UnknownHostException仍然存在。

我们还尝试过在浏览器中打开使用的URL,但该功能例外,即在浏览器中打开了网站。

这是我的测试代码:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class LFTProxyTest {

private static String uname = null;
private static String pass = null;

public static void main(String[] args) {
    System.setProperty("java.net.useSystemProxies", "true");
    // uname = "test"; // whatever that user provides
    // pass = "sectret"; // whatever that user provides
    Authenticator.setDefault(new ProxyAuth(uname, pass));

    contactHost();
    downloadFile();
}

private static boolean downloadFile() {
    System.out.println("CHECK connection");
    int cp = contactHost();
    if (cp == 200)
        return true;
    if (cp == 407)
        return false;
    else {
        try {
            System.out.println("Try loading file: ");
            URL url = new URL("https://www.google.de");
            URLConnection urlConnection = url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            dBuilder.parse(in);
            System.out.println("    FILE DOWNLOAD successfull!");
        } catch (Exception e) {
            System.out.println("    FILE DOWNLOAD failed:");
            System.out.println("***EXCEPTION: " + e.getMessage());
            return false;
        }
    }
    System.out.println("CHECK done");
    return true;
}

private static int contactHost() {
    HttpClient client = HttpClientBuilder.create().build();// new DefaultHttpClient();
    String catalogURI = "https://api.myserver.de/query";
    HttpGet request = new HttpGet(catalogURI);
    try {
        int ret = 0;
        HttpResponse response = client.execute(request);
        ret = response.getStatusLine().getStatusCode();
        System.out.println("PROXY test: " + ret);
        ((CloseableHttpClient) client).close();
        return ret;
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    }
}

}

我不知道该怎么做,我什至不知道错误可能在哪里。任何想法都将受到高度赞赏!

2 个答案:

答案 0 :(得分:0)

UnknownHostException指定一个非常简单的问题。您尝试访问的远程主机的IP地址无法解析。因此,解决方案非常简单。您应该检查Socket(或引发UnknownHostException的任何其他方法)的输入,并验证它是否为预期的输入。如果您输入的主机名不正确,则可以启动UNIX终端并使用nslookup命令(以及其他命令)来查看DNS服务器是否可以成功将主机名解析为IP地址。

如果您使用的是Windows,则可以使用host命令。如果这不能正常工作,则应检查主机名是否正确,然后尝试刷新DNS缓存。如果还是不行,请尝试使用其他DNS服务器,例如Google Public DNS是一个很好的选择。

答案 1 :(得分:0)

好吧,所以在进一步研究之后,我发现org.apache.http.client.HttpClient根本不尊重java.net.useSystemProxies,无论是通过System还是通过-D进行设置。而且它也忽略了http.proxyHost等。解决方案是使用像这样的ProxySelector:

    ProxySelector.setDefault(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            ArrayList<Proxy> list = new ArrayList<Proxy>(); 
            list.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy1.de", 8000)));
            list.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy2.de", 8080)));
            return list;
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            logger.error("Error in ProxySelector, connection Failed: ", ioe);
        }
    });

我现在遇到另一个异常,但是我可能为此打开另一个线程。