Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.net.UnknownHostException_Java_Url_Dns_Htmlunit_Host - Fatal编程技术网

“线程中的异常”;“主要”;java.net.UnknownHostException

“线程中的异常”;“主要”;java.net.UnknownHostException,java,url,dns,htmlunit,host,Java,Url,Dns,Htmlunit,Host,我正在尝试使用HtmlUnit 2.11从网站下载一个文件。然而,我有一种未知的感觉。下面是代码和完整的堆栈跟踪: 代码: 异常跟踪: Exception in thread "main" java.net.UnknownHostException: 340bopais.hrsa.gov at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAll

我正在尝试使用HtmlUnit 2.11从网站下载一个文件。然而,我有一种未知的感觉。下面是代码和完整的堆栈跟踪:

代码:

异常跟踪:

Exception in thread "main" java.net.UnknownHostException: 340bopais.hrsa.gov
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.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(分组互联网浏览器)是一种ICMP(互联网控制管理协议)协议

HTTPS是一种传输协议

许多网络提供商和服务管理者仅为必要的协议和端口限制对其资源的访问

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


更新:

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

  • 使用Eclipse创建maven项目

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

  • 下载
    chrome.exe
    驱动程序,并将其放入驱动程序文件夹

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

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
    

  • 它就像一个符咒

    我想这个网站安全证书有问题,我已经试着运行你的URL
    https://340bopais.hrsa.gov/reports
    从浏览器

    默认情况下,如果服务器的证书链无法验证且以前未在信任库中安装,则使用URL类访问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) {
    }
    

    您能从命令提示符ping该URL吗?它不能确定URL的IP地址:
    https://340bopais.hrsa.gov/reports
    @khAn,我尝试了以下命令:ping 340bopais.hrsa.gov。响应为:Ping请求找不到主机340bopais.hrsa.gov。请检查名称,然后重试。此外,“tracert 340bopais.hrsa.gov”给出了以下结果:无法解析目标系统名称340bopais.hrsa.gov。为什么使用
    INTERNET EXPLORER_8
    ?此网站的安全证书存在问题。如果URL在浏览器中运行..这是@Salman的下一部分。代码无法连接到网站本身。嗨@Salman,请告诉我如何使用Java连接到该网站?嗨@Salman,我没有使用Selenium;但你仍然可以发布你的答案。我会努力实现它。谢谢@Salman。看起来你是个Java书呆子。明天我会试试你的解决办法。我希望它与Java 6兼容,因为这是我们服务器上可用的最新版本。太棒了@Salman!!!!非常感谢你。。。这对我很有效。只是我不得不使用低版本的Selenium。此外,我还必须在Maven依赖项中明确提到org.springframework.boot.autoconfigure的jar。它在我的机器上运行得很好。我必须在Unix服务器上运行它。如果我在那里遇到问题,我将单独处理。但是,是的,您给了我正确的解决方案。谢谢@Amol Raje,我甚至将网站的ssl证书添加到Java密钥库中。不过,我明天会试试你的解决方案,看看。你好@Amol Raje,我用了你的代码。然而,同样的例外也来了。同样的问题@Amol
            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();
    
    
    
        }
    
    // 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) {
    }