在 Jsoup 中获取响应代码时,无法将 HttpConnection 转换为 Connection$Response

HttpConnection cannot be cast to Connection$Response while taking response code in Jsoup

我正在使用 Jsoup API 1.8.3 解析使用 PHP 生成的网站中存在的所有链接。主页、页面等联系表单已成功解析。但对于登录页面,由于以下原因失败:

HTTP error fetching URL. Status=404, https://.../info/en/loginMf.php?src=trading

它失败了,因为它需要有效的凭据。因此我想跳过这样的 URLs。 我正在尝试通过使用以下方法检查状态代码来完成它:

Connection.Response response=(Response) Jsoup.connect(path);//Added typecast
  System.out.println(response.statusCode());

但是这个添加的类型转换在运行时给出了错误:classCastException

在将 URL 命中传递给 parse() 方法之前获取状态代码的确切方法是什么?

编辑:

我尝试采用@lonesome给出的答案,如下:

        try
          {
            Connection.Response response= Jsoup.connect(path).execute();
            int statusCode=response.statusCode();
            if (statusCode <= 200 && statusCode < 300) {
                 doc = Jsoup.connect(filename).get();//web crawling
                  }

          } 
                 catch(HttpStatusException http)
                 {
                     System.out.println("Status:"+http.getStatusCode());
                     http.printStackTrace();
                 }

但问题是,int statusCode=response.statusCode(); 行没有被执行。这可能是因为 jsoup 的工作方式。它需要执行以发回@lucksch 回答的响应。

只有当您实际向您想要的网站发出请求时,您才会得到响应。这就是您获得它的方式:

Connection.Response response= Jsoup.connect(path).execute();

execute 方法 returns a Connection.Response,其中包含状态代码。

JSoup 在返回不正确的 HTTP 响应时抛出 HttpStatusException。这是一个演示程序,将向您展示如何使用 JSoup 正确验证 url。我建立了一个 url 列表,当然你已经从某个地方得到了那个列表。

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;

public class JSoupMain
{
    public static void main(String[] args)
    {
        List<String> allUrls = new ArrayList<String>();
        allUrls.add("http://en.wikipedia.org");
        allUrls.add("http://en.wikipedia.org/blah"); //<---This will cause a 404 status code to be returned
        allUrls.add("http://mvnrepository.com/artifact/org.jsoup/jsoup/1.8.3");

        System.out.println("Checking urls");
        List<String> goodUrls = getGoodUrls(allUrls);

        System.out.println("\r\nGood urls");
        for(String url : goodUrls)
        {
            System.out.println(url);
        }
    }

    private static List<String> getGoodUrls(List<String> allUrls)
    {
        List<String> goodUrls = new ArrayList<String>();
        for(String url : allUrls)
        {
            try
            {
                Jsoup.connect(url).get();
                goodUrls.add(url);
            }
            catch(HttpStatusException e)
            {
                System.out.println("Url " + url + " resulted in " + e.getStatusCode());
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        return goodUrls;
    }
}

试试这个:

                  HttpURLConnection httpConn;
                  URL url = new URL("adr");

                  URLConnection connection = url.openConnection();
                  int statusCode = httpConn.getResponseCode();

                   if (connection instanceof HttpURLConnection) {
                      try{
                          httpConn = (HttpURLConnection) connection;

                     if (statusCode <= 200 && statusCode < 300) {
                             // means the connection was successful
                             //do crawling
                       }
             }
     } 
                   catch (ConnectException ex) { java.util.logging.Logger.getLogger(crawler.class.getName()).log(Level.SEVERE, null, ex);}  //catch the possible exception.
                   catch (SSLHandshakeException |SocketException | SocketTimeoutException | UnknownHostException ex) {java.util.logging.Logger.getLogger(crawler.class.getName()).log(Level.SEVERE, null, ex);
                   //replace crawler with the name of your program main class