It seems your situation is how you want to establish a connection with jsoup, then check the status code, and then according to the status code that you will analyze, or what you want to do.
To do this, you first need to check the URL status code, and then create a connection.
Response response = Jsoup.connect("Your Url ").followRedirects(false).execute(); System.out.println(response.statusCode() + " : " + response.url());
response.statusCode()
will return you a status code
After that you can create your own connection
if (200 == response.statusCode()) { doc = Jsoup.connect(" Your URL").get(); Elements elements = doc.select("href"); }
Your class will look like this
package com.demo.soup.core; import java.io.IOException; import org.jsoup.Connection.Response; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class DemoConnectionWithJsoup { public static void main(String[] args) { Response response; try { response = Jsoup.connect("Your URL ").followRedirects(false).execute(); if (200 == response.statusCode()) { Document doc = Jsoup.connect("Your URL").get(); } } catch (IOException e) { e.printStackTrace(); } } }
source share