【问题标题】:Unable to obtain the response code! Pointers?无法获取响应码!指针?
【发布时间】:2011-04-22 16:17:30
【问题描述】:

我正在尝试抓取 300,000 个网址。但是,在中间的某个地方,当尝试从 URL 检索响应代码时,代码会挂起。我不确定发生了什么问题,因为正在建立连接,但之后问题就出现了。我已经按照建议修改了设置读取超时和请求属性的代码。但是,即使现在代码也无法获取响应代码! 任何建议/指针将不胜感激。另外,有没有办法在某个时间段内 ping 一个网站,如果它没有响应,就继续下一个?

这是我修改后的代码sn-p:

URL url=null;

try
{
   Thread.sleep(8000);
}
catch (InterruptedException e1)
{
   e1.printStackTrace();
}

 try
{
   //urlToBeCrawled comes from the database
   url=new URL(urlToBeCrawled);
}
catch (MalformedURLException e)
{
   e.printStackTrace();
 //The code is in a loop,so the use of continue.I apologize for putting code in the catch block.
  continue;
}
 HttpURLConnection huc=null;
 try
{
   huc = (HttpURLConnection)url.openConnection();

}
catch (IOException e)
{
   e.printStackTrace();
}
 try
 {
    //Added the request property
   huc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
  huc.setRequestMethod("HEAD");

 }
 catch (ProtocolException e)
 {
    e.printStackTrace();
 }

 huc.setConnectTimeout(1000);
 try
 {
    huc.connect();

  }
 catch (IOException e)
 {

    e.printStackTrace();
    continue;
  }

 int responseCode=0;
 try
 {
   //Sets the read timeout
   huc.setReadTimeout(15000);
   //Code hangs here for some URL which is random in each run
   responseCode = huc.getResponseCode();

  }
 catch (IOException e)  
{
   huc.disconnect();

   e.printStackTrace();
   continue;
}
if (responseCode!=200)
{
   huc.disconnect();
   continue;
 }

【问题讨论】:

    标签: java url web-crawler http-response-codes


    【解决方案1】:

    它挂起是因为字节流中从未收到响应代码。您将需要查看 http 调试器并查看实际收到的内容(如果有的话)。但是,它似乎确实打开了与服务器的 TCP 连接。它可能不像您的用户代理(可能没有设置为您认为的那样)或HEAD 的请求方法,或者它可能是带宽有限的服务器。您可以使用Socket 类来打开一个连接并手动准备好字节以查看您正在/没有收到什么。

    附带说明,仅使用Socket 实际上并不是一个坏方法取决于您想要做什么听起来就像您正在编写一个 http 服务器检查器,在这种情况下,您将通过直接使用 Socket 获得更多功能,因为您将能够设计出更好、更优化的技术(您是毕竟要处理大量的低级网络 io)。

    【讨论】:

      【解决方案2】:

      在调用打开连接的 url.openConnection() 之后,您正在 HttpURLConnection 上设置读取和连接超时。因此它们没有生效。为此,我可能会使用 Jetty HttpClient 而不是 Java URL 类。

      回答你的第二点。是的,只需尝试在远程域名上打开与端口 80(或 URL 中指定的其他端口)的连接,您可以使用原始套接字从 URL(使用url.getHost())中提取该连接。为此,我将使用 Netty 而不是 Java 套接字。

      【讨论】:

      • 感谢您的回复!我会调查他们
      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2018-05-22
      相关资源
      最近更新 更多