[ale] OT: Java and Cookies

Jason Day jasonday at worldnet.att.net
Fri Aug 29 15:52:12 EDT 2003


On Fri, Aug 29, 2003 at 01:47:20PM -0400, Christopher Fowler wrote:
> 
> 
> I'm trying to do a POST and grab a cookie to be used for later 

One of the things my company does is send XML via HTTP to different
hosts.  I wrote the code for a java client that logs in to a Weblogic
app server using forms-based authentication.  I spent many, many
frustrating days getting it to work right, so if it's at all possible, I
would recommend using Basic authentication or some other means.  Keep in
mind that if there is every anything in between your client and the
server, like a proxy, firewall, load balancer, SSL accelerator, etc.
then that will exponentially complicate things for you.

[snip]
>             connection.setDoOutput(true);
>             connection.setDoInput(true);
>             printout = new DataOutputStream (connection.getOutputStream ());
>             printout.writeBytes (emit);
>             printout.flush ();
>             printout.close ();

Try commenting out the close, and check the response code with
connection.getResponseCode() before trying to access any headers.

>             c = connection.getHeaderField("Set-Cookie");

I seem to remember having problems with the getHeaderField method.  Try
iterating over all the headers and comparing the key.  E.g.:

    String key = connection.getHeaderFieldKey (0);
    for (int i = 0; key != null; i++) {
        if (key.equals ("Set-Cookie")) {
            c = connection.getHeaderField (i);
            // do something with cookie, add a break if done
        }
        
        key = connection.getHeaderFieldKey (i);
    }

Also, keep in mind that the server can set multiple Set-Cookie headers.
This is especially true if the server is behind any kind of load
balancer.  So you will probably need to iterate over all of them any
way.

The HttpClient class that is shipped with the JDK/JRE is a sloppy,
bug-ridden hack, rivalled only by java.util.Date and perhaps
java.io.StreamTokenizer in bad design.  There was a bug in earlier
versions that caused the connection to "lose" headers when used with a
server that used keepalive connections.  This was fixed in version
1.3.1_04 and 1.4, so if you're using an earlier 1.3 version that might
be causing you problems.

HTH,
Jason
-- 
Jason Day                                       jasonday at
http://jasonday.home.att.net                    worldnet dot att dot net
 
"Of course I'm paranoid, everyone is trying to kill me."
    -- Weyoun-6, Star Trek: Deep Space 9
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list