Checking the status of a web page [duplicate]

Mobile Technologies Mobile Computing 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ How to use java.net.URLConnection to fire and handle HTTP requests_x000D_ _x000D_ 11 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ Need to make a program that takes a valid URL of a webpage like www.stackoverflow.com/questions and its IP address equivalent. The program will then find that webpage and return the status code of the page to us such as 200 OK and 404 NOT FOUND. If the webpage isn’t reachable, a message should be returned explaining the situation. Here’s what I have done so far: interface Result { public boolean ok (); public String message (); } class Page { public Result check ( String wholeURL ) throws Exception { throw new Exception ( "Not sure about the rest”); } } Also if I were to check a page like http://www.stackoverflow.com I’ll create an instance of Page and then do something like this: Page page = new PageImplementation (); Result result = page.check ( "http://www.stackoverflow.com:60" ); if ( result.ok () ) { ... } else { ... } The object that is returned is an instance of Result, and the “ok” method should return true when the status code is 200 OK but false otherwise. The method “msg” should return the status code as string.

Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Have a look at the HttpURLConnection class within the JDK or use Apache Http Components. Basically you try to connect to the url and check the response header or wait for a timeout if the server isn't reachable at all. With HttpURLConnection it might look like this: URL url = new URL("http://www.stackoverflow.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.connect(); int httpStatusCode = connection.getResponseCode(); //200, 404 etc.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Mobile Technologies Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community