【问题标题】:Domain not configured on this server此服务器上未配置域
【发布时间】:2015-03-26 14:20:47
【问题描述】:

我正在实现网络爬虫,我正在使用InetAddress 类从域名中获取 IP 地址。我尝试了域名 en.wikipedia.org 并获得了 ip 208.80.154.224。现在我正在尝试使用 jSoup 解析器从该服务器获取page /wiki/Cricket,但出现如下错误

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=http://208.80.154.224/wiki/Cricket
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:459)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
    at OtherClasses.TestDownloadJSoup.main(TestDownloadJSoup.java:30)
Java Result: 1

我获取页面的代码是

Connection con = Jsoup.connect("http://208.80.154.224/wiki/Cricket")
                        .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
                        .timeout(1000*5)
                        .followRedirects(true)
                        .referrer("http://www.google.com");

我应该怎么做才能解决这个 404 错误,即使我在浏览器中写了这个 ip,它给出的域没有在这个服务器上配置错误

【问题讨论】:

    标签: java http jsoup


    【解决方案1】:

    有的服务器,可以实现Virtual hosting,意思是一台服务器(一个IP地址),可以服务多个域名,根据配置决定服务哪个页面。
    您应该在查询中添加Host header

    System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); // this line is important to allow change in the Host header
    Connection con = Jsoup.connect("http://208.80.154.224/wiki/Cricket")
                        .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
                        .timeout(1000*5)
                        .followRedirects(true)
                        .header("Host","en.wikipedia.org") // new entry here
                        .referrer("http://www.google.com");
    

    查看此answer 了解更多信息

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 2013-11-07
      • 2021-04-09
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2021-10-17
      • 2019-09-19
      相关资源
      最近更新 更多