1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.net.URLConnection;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class HtmlOutputLimiter {
  7. private final int maxRetries;
  8. private final int retryDelayMillis;
  9. private final Map<String, Integer> retryCounts = new HashMap<>();
  10. public HtmlOutputLimiter(int maxRetries, int retryDelayMillis) {
  11. this.maxRetries = maxRetries;
  12. this.retryDelayMillis = retryDelayDelayMillis;
  13. }
  14. public String getHtml(String url) throws IOException {
  15. int retryCount = 0;
  16. while (retryCount <= maxRetries) {
  17. try {
  18. return fetchHtml(url);
  19. } catch (IOException e) {
  20. retryCount++;
  21. if (retryCount > maxRetries) {
  22. throw e; // Re-throw if max retries reached
  23. }
  24. try {
  25. Thread.sleep(retryDelayMillis); // Wait before retrying
  26. } catch (InterruptedException ie) {
  27. Thread.currentThread().interrupt(); // Restore interrupted status
  28. throw new IOException("Retry interrupted", ie);
  29. }
  30. }
  31. }
  32. return ""; // Should not reach here
  33. }
  34. private String fetchHtml(String url) throws IOException {
  35. URL htmlUrl = new URL(url);
  36. URLConnection connection = htmlUrl.openConnection();
  37. connection.setConnectTimeout(5000); // Set connect timeout
  38. connection.setReadTimeout(10000); // Set read timeout
  39. String html = connection.getInputStream().toString();
  40. return html;
  41. }
  42. public void incrementRetryCount(String url) {
  43. retryCounts.put(url, (retryCounts.getOrDefault(url, 0) + 1));
  44. }
  45. public static void main(String[] args) throws IOException {
  46. HtmlOutputLimiter limiter = new HtmlOutputLimiter(3, 1000); // 3 retries, 1 second delay
  47. try {
  48. String html = limiter.getHtml("https://www.example.com");
  49. System.out.println(html.substring(0, 200) + "..."); // Print first 200 chars.
  50. } catch (IOException e) {
  51. System.err.println("Failed to fetch HTML after multiple retries: " + e.getMessage());
  52. }
  53. }
  54. }

Add your comment