Ads Inside Post

Search in This Blog

Get cookies in java

How to get cookies in java


import java.io.*;
import java.net.*;
public class CO {
    public static void main(String ss[]) {
     try {
             String url="http://www.mywebsite.com/anything.do";
              HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        System.out.println("\n[HTTP GET RESPONSE]");
        System.out.println("==== Response Headers =====");
        String serverCookies = conn.getHeaderField("Set-Cookie");
        if(serverCookies != null) {
                    String[] cookies = serverCookies.split(";");
                    for(String s : cookies){
                        s = s.trim();
                        if(s.split("=")[0].equals("JSESSIONID")){
                                String J_SESSION_ID = s.split("=")[1];
                                System.out.println(" Session ID Found: " + J_SESSION_ID);
                            }
            }
        }
    } catch(Exception ee) {
        System.out.println(ee.toString());
    }
    }
}