URL status connection 404 using jSoup

0

I am using jSoup to obtain different information, among other links, from which I later extract information. For this I use a method that gives me the code of the connection:

    public static int getStatusConnectionCode(String url) {
    int statusCode = 0; //o algún otro resultado que indique que es error
    try {
        Response response = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(100000).ignoreHttpErrors(true).execute();
        statusCode = response.statusCode();
    } catch (IOException ex) {
        //procura mejorar la forma en que tratas tus excepciones
        System.out.println("Excepción al obtener el Status Code: " + ex.getMessage());
    }
    return statusCode;
}

However, I find that on more than one occasion the code gives me 404, that is, it can not find the page, when I copy and paste the URL I enter perfectly. Is there any way to polish the code and that these things do not happen to me? It also happens randomly, and it is not a problem of internet connection or problem of the page, since from time to time I see in the execution of the program these cases and I test if the page does not really exist.

    
asked by JetLagFox 25.01.2017 в 01:09
source

0 answers