DJ31
DJ31

Reputation: 1239

how to get the negative response code in java

I am using the code as follows in java.

 URL url = new URL("any url");

  HttpURLConnection conn=(HttpURLConnection) url.openConnection();

  InputStream in = conn.getInputStream();

  int responseCode= conn.getResponseCode();

Here I am getting the issue:- for some url,I am getting the negative reponseCode,that is '-1'.and it is getting terminated,it is not going further,so please help me,how to solve or how to handle such errors.

Upvotes: 1

Views: 2570

Answers (2)

Chris
Chris

Reputation: 399

From the JavaDoc of getResponseCode:

It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

Upvotes: 3

oliholz
oliholz

Reputation: 7507

-1 is not a error it returns if no code can be discerned from the response, see here

URL url = new URL("any url");
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
int responseCode = conn.getResponseCode();

if(responseCode == -1) {
    // no code can be discerned
}

Upvotes: 1

Related Questions