This version of www.jasonlam604.com is deprecated.
If you are not redirected to the new web site in 3 seconds, please click on the following link
http://www.jasonlam604.com/v2/index.php
 

 


J2ME Read Cookies
by Jason Lam

// After executing the HttpConnection you can parse out the "set-cookie" values, the
// following method is configured to read cookies from JSP/Servlets, ASP or PHP. It can
// easily be modified to handle other server-side scripts/languages like Cold Fusion
// Zope... etc

private String readCookie( HttpConnection conn ) throws IOException {
    String key;
    String value;
    String[] substrs;

    for( int i = 0; ( key = conn.getHeaderFieldKey( i ) ) != null; ++i ) {
         key = key.toLowerCase();
         if( key.equals( "set-cookie" ) ){
            value = conn.getHeaderField( i );

           while( value != null ) {
                substrs = Utils.split( value, ';' );
                if( substrs[0].startsWith("JSESSIONID=") || // Java
                     substrs[0].startsWith("PHPSESSID") || // PHP
                     substrs[0].startsWith("SessionId") // ASP
                ){
                return substrs[0];
               }
               value = substrs[1];
           }
         }
    }
    return null;
}

 

| Home | Tutorials | Tips / Hints | Books | Links |
| Resume | Portfolio | Services | Contact |

The contents of this webpage are copyright © 2003 - 2006
Jason Lam All Rights Reserved