| 
 
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new 
Artima Forums.
 
Message:
 
 
java - asp maintain session 
 
 
Posted by Anubhav on April 13, 2001 at 3:40 AM 
guys, this is all i could work out .. but it still returns a new session id from the consecutive calls to other pages from java app. see if it helps: import java.net.*; import java.util.*; import java.io.*; public class CookieTest {     Hashtable cookies;  // we will store cookies in this hashtable     public CookieTest()     {         cookies = new Hashtable();     }     public static void  main( String args[] )     {         if( args.length != 1 )         {             System.out.println( "Usage: java CookieTest url" );         }         else         {             try{                 CookieTest ct = new CookieTest(  );                 URL u = new URL( args[0] );                 System.out.println( "Connecting the first time..." );                 ct.connectToURL( u ); 				u=new URL("http://localhost:8100/webidentity/test2.jsp"); 				//u = new URL( "http://192.168.0.101/pm/old/anutest2.asp");                 System.out.println( "Connecting the second time..." );                 ct.connectToURL( u ); /* 				u = new URL( args[0] );                 System.out.println( "Connecting the third time..." );                 ct.connectToURL( u );*/             }             catch( MalformedURLException    ex )             {                 System.out.println( "Malformed url: " + args[0] );             }         }         System.exit(0);     }     public void connectToURL(  URL u )     {         try{             // get a URLConnection             URLConnection uconn = u.openConnection();             // set any cookies previously   saved in the cookies Hashtable //            uconn.setDoOutput(true); //            uconn.setDoInput(true);             setCookies( uconn );             // make a   connection, this will send headers to the server             uconn.connect();            // get cookies and save them in the cookies Hashtable             saveCookies( uconn  );             InputStream in=uconn.getInputStream();             BufferedInputStream bufIn = new BufferedInputStream(in); //            strFileInput=new StringBuffer();             // Repeat until end of file             for (;;)             {                 int data = bufIn.read();                 // Check for EOF                 if (data == -1)                     break;                 else                 {                     System.out.print ( (char) data); //                    strFileInput.append((char) data); 				}             }  //			uconn.disconnect();         }         catch( IOException ex )         {             System.out.println( "Couldn't connect to " + u.toString() );         }     }
     private void setCookies( URLConnection urlconn )     {         String headerfield = "";         String host = "";         // get the host         host = urlconn.getURL().getHost();         // get saved cookies from hashtable         headerfield = (String)cookies.get( host );         // check if there are any saved cookies         if( headerfield != null )         {             System.out.println( "Setting cookies: " + headerfield );             // set cookie string as request property             urlconn.setUseCaches(false); 			urlconn.setRequestProperty("Connection","Keep-Alive"); 			urlconn.setRequestProperty("Host","localhost:8100"); 			urlconn.setRequestProperty("Accept-Encoding","gzip, deflate"); 			urlconn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"); 			urlconn.setRequestProperty("Accept-Language","en-us");             urlconn.setRequestProperty( "Referer","http://192.168.0.101/pm/old/anutest.asp");             urlconn.setRequestProperty( "Cookie", headerfield );         }   }   private void saveCookies( URLConnection urlconn )   {     int i = 0;     String key = null;     String cookie = null;     String host = null;     String headerfield = null;     StringTokenizer tok = null;     // get the host     host = urlconn.getURL().getHost();     // forward pass any starting null values     while( urlconn.getHeaderFieldKey( i ) == null ) i++;     // check all the headerfields until there are no more     while( urlconn.getHeaderFieldKey( i ) != null )     {         key = urlconn.getHeaderFieldKey( i );         // check if it is a Set-Cookie header         if( key.equalsIgnoreCase( "set-cookie" ) )         {             headerfield = urlconn.getHeaderField( i );             if( headerfield != null ) // can the headerfield be null here ?             {                 // parse out only the name=value pair and ignore the rest                 tok = new StringTokenizer( headerfield , ";" , false );                // if this is anything but the first cookie add a semicolon and a space                 // before we add the next cookie.                 cookie = ( cookie != null ? cookie + "; " + tok.nextToken() :  tok.nextToken() );             }         }         i++;     }    // save the cookies in our cookie collection with the hostname as key     if( cookie != null )     { 		try{       System.out.println( "Saving cookies: " + cookie );       System.in.read();       System.in.read();       cookies.put( host, cookie );   		}   		catch(Exception ioe){}     }   } } // end class
 
 
 
  
Replies:
 
 |