Return 0 from HTTP

Asked

Viewed 257 times

0

I’m having a bit of a strange problem..

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

The algorithm, takes as a parameter a URL, then returns me the HTTP status. If so 200, I know you’re all right, if not, I’ll deal with it depending on the return.. Well, when I pass as parameters some specific sites it returns me the value 0 (Which is no HTTP status)

Could someone help me ?

Follow the code:

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;
}
  • Change your variable code to 3 and test with a site that was previously returning 0. Return works or becomes three?

  • Hello Lucas, I changed. and the return became 3

2 answers

1

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

Try doing this with a site that you know works and returns 200. Then see with one that you know doesn’t work.

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

0


I managed 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

Thank you all

Browser other questions tagged

You are not signed in. Login or sign up in order to post.