Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection  RSS feed

 
Jess Ishasder
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is part of my program:
===============
try{
HttpURLConnection con = (HttpURLConnection) (new URL("http://www.healthnet.org.np/java/msg00519.html")).openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println("File Not Found");
}
else{
System.out.println("code : " + con.getResponseCode());
}
}
catch(FileNotFoundException fnf){
System.out.println("Problem!!!");
}
====================================
Suppose the link provided should be "File Not Found".
The problem is why I can't check this out by
getResponseCode()? I have used getResponseCode() to check for file that existed, it performed well.
-How can I check for "File Not Found"?
-How can I check for redirection of a page?
Thanks for your help!!!
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jess Ishasder:

The problem is why I can't check this out by
getResponseCode()? I have used getResponseCode() to check for file that existed, it performed well.
-How can I check for "File Not Found"?

what http status code did you get?

-How can I check for redirection of a page?

you cvan try this
...
con.setInstanceFollowRedirects(false);
...
boolean redirected = con.getResponseCode() >= 300 && con.getResponseCode() < 400;
 
Jess Ishasder
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get any code returned, only an exception is caught!!!
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jess Ishasder:
I can't get any code returned, only an exception is caught!!!

which exception did you get? catch IOException and check the actual type, just use a System.out.println(ex).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!