1. import java.io.IOException;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.concurrent.ConcurrentHashMap;
  7. public class HTMLMonitor {
  8. private static final ConcurrentHashMap<String, MonitorResult> monitorResults = new ConcurrentHashMap<>();
  9. public static void main(String[] args) {
  10. // Example usage: Monitor a few URLs
  11. monitor("https://www.example.com");
  12. monitor("https://www.google.com");
  13. monitor("https://invalid-url.example"); // Example of an invalid URL
  14. }
  15. /**
  16. * Monitors an HTML document at the given URL.
  17. *
  18. * @param url The URL of the HTML document to monitor.
  19. */
  20. public static void monitor(String url) {
  21. try {
  22. URL absoluteUrl = new URL(url); // Create a URL object
  23. monitorDocument(absoluteUrl.toString());
  24. } catch (MalformedURLException e) {
  25. logError("Invalid URL: " + url, e);
  26. }
  27. }
  28. /**
  29. * Monitors a single HTML document.
  30. *
  31. * @param url The URL of the HTML document.
  32. */
  33. public static void monitorDocument(String url) {
  34. try {
  35. long startTime = System.currentTimeMillis();
  36. String response = fetchHtmlDocument(url);
  37. long endTime = System.currentTimeMillis();
  38. MonitorResult result = new MonitorResult(url, response, endTime - startTime);
  39. monitorResults.put(url, result);
  40. // Further processing of the HTML content could be done here (e.g., parsing).
  41. System.out.println("Monitored: " + url + " - Duration: " + (endTime - startTime) + "ms");
  42. } catch (IOException e) {
  43. logError("Error monitoring document: " + url, e);
  44. }
  45. }
  46. /**
  47. * Fetches the HTML document from the given URL.
  48. * @param url The URL to fetch
  49. * @return The HTML content as a string.
  50. * @throws IOException if an error occurs during the fetch.
  51. */
  52. private static String fetchHtmlDocument(String url) throws IOException {
  53. java.net.HttpURLConnection connection = (java.net.HttpURLConnection) new URL(url).openConnection();
  54. connection.setReadTimeout(5000); // Set a timeout of 5 seconds
  55. connection.setConnectTimeout(5000);
  56. connection.getInputStream(); // Force connection
  57. return connection.getText();
  58. }
  59. /**
  60. * Logs an error message to the console.
  61. *
  62. * @param message The error message.
  63. * @param e The exception that caused the error.
  64. */
  65. private static void logError(String message, Exception e) {
  66. System.err.println("ERROR: " + message);
  67. e.printStackTrace();
  68. }
  69. /**
  70. * Inner class to store the results of monitoring a document.
  71. */
  72. static class MonitorResult {
  73. String url;
  74. String htmlContent;
  75. long duration;
  76. public MonitorResult(String url, String htmlContent, long duration) {
  77. this.url = url;
  78. this.htmlContent = htmlContent;
  79. this.duration = duration;
  80. }
  81. }
  82. }

Add your comment