HTTP return 0

0

I'm having a rather strange problem ..

I need to return the status of some web addresses for the application, so I used the code below:

The algorithm takes a URL as its parameter, so it returns me the HTTP status. If it's 200 , I know it's okay, if not , I'll handle it depending on the return .. Well, when I pass as a few specific site parameters it returns the value 0 (which is not an HTTP status)

Can anyone help me?

Follow the code below:

public static Integer verificarSistema(String enderecoUrl){
    Integer code =0;
    try {
        URL url = new URL(enderecoUrl);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        code = urlConnection.getResponseCode();   
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        in.close();
        urlConnection.disconnect();
    } catch (MalformedURLException e){
        System.out.println("Erro ao criar URL. Formato inválido.");
    } catch (IOException e2) {     
    }
    return code;
}
    
asked by anonymous 02.05.2017 / 15:55

2 answers

0

I was able to solve the problem ..

It turns out that in the company where I work, there were network blocks for certain addresses, so it was not possible to make the connection

Thanks to everyone

    
08.05.2017 / 19:30
1

Then, as we saw in the comment, the returned error changes depending on the value fed initially. There is no HTTP error with code 0 or 3. The numbers you are seeing are returned by your code or your library. You need to check the documentation and perform a debug to see when it happens.

Try to do this with a website that you know works and returns 200. Then look at one that you know does not work.

I would venture to say that it could be a syntax error, or even in the URL, since to return 0 it would not even enter the block try-catch

    
02.05.2017 / 16:14