Problem:
CookieManager stores cookies in the CookieStore after visiting urls, but it does not seem to retrieve cookies from the cookiestore when creating http requests.
It this a bug ?
To test cookies, I made a simple php page that displays and sets cookies:
http://www.vitaminb5acne.com/testcookies.php
The Java HTTP client code that I use to test CookieManager:
import java.io.*;
import java.net.*;
import java.util.*;
// Retrieves a webpage two times, print it, and print cookies
public class Fetch {
public static void main(String args[]) throws Exception {
// Setup the cookiemanager
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
// Visit the url for the first time
// (so that cookies will be added to the CookieStore afterward)
System.out.println("--- First visit ---");
visitPage();
// Visit the url for second time
// (So that cookies *SHOULD* be retrieved from the CookieStore
// when creating the HTTP request.)
System.out.println("--- Second visit ---");
visitPage();
// Print the stored cookies
System.out.println("--- Printing stored cookies ---");
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println("Cookie: " + cookie);
}
}
// Visits a webpage, and prints it
public static void visitPage() throws Exception {
String urlString = "http://www.vitaminb5acne.com/testcookies.php";
// retrieve page
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
Object obj = connection.getContent();
// Print webpage
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
Executing this code, shows that the webpage returns cookies, but that cookies aren't retrieved from the CookieStore of CookieManager when making a second HTTP request.....
Does anybody know what is going wrong?