I'm writing a program to send a POST method to a website and then login. But for a successful login the website needs to store and retrieve cookies.
This is the code which I'm using to send POST method to the website:
URL url = new URL(link);
CookieHandler.setDefault(new CookieManager());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(values);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
writer.close();
reader.close();
So how can I handle the cookies?