线程异常 "main" java.net.UnknownHostException

Exception in thread "main" java.net.UnknownHostException

我正在尝试使用 HtmlUnit 2.11 从网站下载文件。但是,我得到 UnknownHostException。下面是代码和完整的堆栈跟踪:

代码:

final WebClient webClient = new WebClient(
                BrowserVersion.INTERNET_EXPLORER_8);

        URL Url = new URL("https://340bopais.hrsa.gov/reports");

        HtmlPage page = webClient.getPage(Url);
        HtmlSubmitInput button = page
                .getElementByName("ContentPlaceHolder1_lnkCEDailyReport");

        final HtmlPage page2 = button.click();

异常跟踪:

Exception in thread "main" java.net.UnknownHostException: 340bopais.hrsa.gov
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:171)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1484)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1402)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:304)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
    at src.main.java.DataDownloader.main(DataDownloader.java:30)

PING(Packet Internet Groper)是一种 ICMP(Internet 控制管理协议)协议。

HTTPS 是一种传输协议。

许多网络供应商和服务管理者只允许必要的协议和端口访问他们的资源。

托管 340bopais.hrsa.gov 的组织很可能已将防火墙和其他网络基础设施配置为仅允许端口 80 和 443 上的 TCP 流量到达其服务器。


更新:

我成功地使用 java 和 selenium 下载了文件。我把整个代码做了一个repository,你可以下载我的代码。但在这里我向您解释如何使用它:

  1. 用你的Eclipse做一个maven项目

  2. 将名为 driver 的文件夹添加到 resource 文件夹中

  3. 下载thischrome.exe驱动,放入驱动文件夹

  4. 将此依赖项添加到您的 pom.xml:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
    
  5. 进入main方法类型:

    public static void main(String[] args) {

            File file = new 
                           File(StackApplication.class.getClassLoader().getResource("driver/chromedriver.exe").getFile());
                String driverPath=file.getAbsolutePath();
                System.out.println("Webdriver is in path: "+driverPath);
                System.setProperty("webdriver.chrome.driver",driverPath);
    
                WebDriver driver=new ChromeDriver();
                driver.navigate().to("https://340bopais.hrsa.gov/reports");
                driver.findElement(By.xpath("//*[@id=\"headingTwo\"]/h4/a")).click();
                driver.findElement(By.xpath("//*[@id=\"ContentPlaceHolder1_lnkCEDailyReport\"]")).click();
    
    
    
        }
    

而且效果很好

我认为此网站安全证书有问题我已尝试从浏览器 运行 您的 URL https://340bopais.hrsa.gov/reports

默认情况下,如果服务器的证书链之前未安装在信任库中,则使用 URL class 访问 HTTPS URL 会导致异常。如果您想出于测试目的禁用证书验证,则需要使用信任所有证书的信任管理器覆盖默认信任管理器。

尝试这可能会解决您的问题:

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
 new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    public void checkClientTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {
    }
    public void checkServerTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {
    }
 }
};

// Install the all-trusting trust manager
try {
  SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  } catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore
try {
  URL url = new URL("https://yourwebsite/example.html");
 } catch (MalformedURLException e) {
}