尝试连接到网站时使用GAE应用程序的未知主机异常

时间:2018-03-14 22:21:36

标签: java eclipse google-app-engine htmlunit

我正在学习如何在Java 8中使用htmlUnit,我试图将应用程序部署到谷歌应用程序引擎,这将经常刮掉某些网​​站。我正在Eclipse中开发应用程序,它在本地运行时按预期工作,但是在部署到GAE后,我的应用程序无法再与任何网站建立连接。

 try (final WebClient webClient = new WebClient()) {

      webClient.setCookieManager(new CookieManager() {
          protected int getPort(final java.net.URL url) {
          final int r = super.getPort(url);
          return r != -1 ? r : 80;
          }
          });  

      final HtmlPage page = webClient.getPage("https://www.google.com");
      }

  catch(Exception e){
      System.out.println(e.getMessage());         
  }

错误发生在" webClient.getPage(....)"

java.net.UnknownHostException: www.google.com

部分堆栈跟踪:

[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>: java.lang.RuntimeException: java.net.UnknownHostException: www.recreation.gov
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.UrlFetchWebConnection.getResponse(UrlFetchWebConnection.java:162)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1394)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1312)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:396)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:317)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:465)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at pack.HelloAppEngine.doGet(HelloAppEngine.java:49)
[s~permitseacherbpd/20180314t161057.408306947286449649].<stderr>:   at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)

我尝试访问的任何网站都会出现此错误,并且不是htmlUnit独有的,因为我在其他项目之前遇到过此错误。为什么我在部署到应用引擎后无法连接?

1 个答案:

答案 0 :(得分:1)

我的小测试servlet也开始抛出类似的UnknownHostException。我遇到#63916008链接到注意到的<url-stream-handler> documentation(强调我的)

  

对于Java 8运行时,默认值为native,这意味着标准Java网络类使用标准Java HTTP(S)传输,如Java 8运行时与Java 7行为中所述。 此设置要求应用启用结算,否则请求会产生以下运行时错误:

     

的java.net.UnknownHostException
  java.net.SocketTimeoutException
  java.io.IOException的

appengine-web.xml配置为使用urlfetch,您的问题应该得到解决!