Reputation: 1239
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
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
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