[ale] OT: Java and Cookies

Christopher Fowler cfowler at outpostsentinel.com
Fri Aug 29 13:47:20 EDT 2003




I'm trying to do a POST and grab a cookie to be used for later 
authentication. I hope that maybe you guys have done this
before.  The aut.asp page is a simple POST form.  When the
form is submitted it returns a frameset and a cookie.  I can
not seem to get the the HttpURLConnection to get the cookie.  It
seems that the conneciton does the post and then tries to
receive each page of the framset.  However, I do not see
and Cookies: being sent with those GET requests.  Is there a
way to get this code to use the Set-Cookie that is being sent
back.   

---------- Cut Here --------------------------------

    public static void main(String args[]) {
        InputStream is;
        int data;
        String s;
        DataOutputStream    printout;
        String c;
        try {
            URL url = new URL("http://127.0.0.1/auth.asp");
            HttpURLConnection connection = (HttpURLConnection)u.openConnection();
            String emit = "login=super&password=smart&action_login=Login";
            connection.setRequestProperty("Content-Length", "" + emit.length());
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setUseCaches (false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setFollowRedirects(false);
            /* Not used now
            connection.setRequestProperty(
                "Authorization", 
                "Basic " + "cm9vdDpwYXNzd29yZA=="
            );      
            */
            connection.setDoOutput(true);
            connection.setDoInput(true);
            printout = new DataOutputStream (connection.getOutputStream ());
            printout.writeBytes (emit);
            printout.flush ();
            printout.close ();
            c = connection.getHeaderField("Set-Cookie");
            System.out.println("Cookie: " + c);
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            System.out.flush();
            while((s = br.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception Exp) {
            System.err.println("Error: " + Exp.getMessage());
            System.exit(1);
        }
    }


---------- Cut Here --------------------------------
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list