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);
}
}