Trying to get initial cookie value for a non authenticated website but it's throwing 403, using HttpURLConnection
Asked Answered
I

0

0

I've requirement to get initial cookie value from website https://www.valueresearchonline.com/ and use it for subsequent requests using JSOUP ( in header I need to pass cookie value )

Below program is always throwing 403, how to fix this ?

    String cookies = "";
    String url = "https://www.valueresearchonline.com/";


    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

    connection.setRequestProperty("User-Agent", "Mozilla/5.0");
    connection.setRequestProperty("Referer", "https://www.valueresearchonline.com/");

    int responseCode = connection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) {
        // Get the cookies from the response headers
        Map<String, List<String>> headerFields = connection.getHeaderFields();
        List<String> cookieHeaders = headerFields.get("Set-Cookie");

        if (cookieHeaders != null) {
            // Join cookie values into a single string
            cookies = String.join("; ", cookieHeaders);
        }
    }
Ian answered 21/12, 2023 at 2:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.