1. import java.net.CookiePolicy;
  2. import java.net.http.HttpClient;
  3. import java.net.http.HttpRequest;
  4. import java.net.http.HttpResponse;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.concurrent.ConcurrentHashMap;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.ScheduledExecutorService;
  10. import java.util.concurrent.TimeUnit;
  11. public class CookieSessionParser {
  12. private static final int RETRY_INTERVAL = 5; // Seconds
  13. private static final int MAX_RETRIES = 3;
  14. private final HttpClient client = HttpClient.newHttpClient();
  15. private final ConcurrentHashMap<String, String> sessionCookies = new ConcurrentHashMap<>(); // Store session cookies, key = domain, value = cookie string
  16. private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  17. public static void main(String[] args) throws Exception {
  18. CookieSessionParser parser = new CookieSessionParser();
  19. // Example usage: Replace with your target URL.
  20. String targetUrl = "https://example.com";
  21. parser.parseSessionCookies(targetUrl);
  22. }
  23. public void parseSessionCookies(String targetUrl) {
  24. try {
  25. //Initial Cookie Retrieval
  26. String responseBody = getSessionCookies(targetUrl);
  27. if (responseBody != null) {
  28. parseCookiesFromResponse(responseBody);
  29. }
  30. } catch (Exception e) {
  31. System.err.println("Error during initial cookie retrieval: " + e.getMessage());
  32. }
  33. }
  34. private String getSessionCookies(String url) throws Exception {
  35. // Create an HTTP request
  36. HttpRequest request = HttpRequest.newBuilder()
  37. .uri(java.net.URI.create(url))
  38. .cookiePolicy(CookiePolicy.ACCEPT) // Accept all cookies
  39. .build();
  40. // Send the request and get the response
  41. HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  42. // Check for errors
  43. if (response.statusCode() >= 400) {
  44. System.err.println("Request failed with status code: " + response.statusCode());
  45. return null;
  46. }
  47. return response.body();
  48. }
  49. private void parseCookiesFromResponse(String responseBody) {
  50. // Extract cookies from the response body. This is a placeholder.
  51. // In a real application, you'd use a proper HTML parser (e.g., Jsoup)
  52. // to extract cookies. This is a simplified example.
  53. String[] cookies = responseBody.split("Cookie: ");
  54. if (cookies.length > 1) {
  55. String cookieString = cookies[1];
  56. String domain = extractDomain(cookieString);
  57. if (domain != null) {
  58. sessionCookies.put(domain, cookieString);
  59. System.out.println("Found cookies for domain: " + domain);
  60. }
  61. }
  62. }
  63. private String extractDomain(String cookieString) {
  64. // Simple domain extraction. Improve this for more robust parsing.
  65. int firstEqual = cookieString.indexOf('=');
  66. if (firstEqual != -1) {
  67. return cookieString.substring(0, firstEqual); // Extract domain part
  68. }
  69. return null;
  70. }
  71. public void scheduledCookieUpdate(String targetUrl) {
  72. scheduler.scheduleAtFixedRate(() -> {
  73. try {
  74. String responseBody = getSessionCookies(targetUrl);
  75. if (responseBody != null) {
  76. parseCookiesFromResponse(responseBody);
  77. }
  78. } catch (Exception e) {
  79. System.err.println("Error during cookie update: " + e.getMessage());
  80. }
  81. }, 0, RETRY_INTERVAL, TimeUnit.SECONDS);
  82. }
  83. public static void shutdown() {
  84. scheduler.shutdown();
  85. try {
  86. scheduler.awaitTermination(5, TimeUnit.SECONDS);
  87. } catch (InterruptedException e) {
  88. System.err.println("Interrupted while waiting for scheduler termination.");
  89. }
  90. }
  91. }

Add your comment