tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kyle.w.willk...@accenture.com
Subject the proper cookie to pass for session id
Date Fri, 21 Sep 2001 15:17:15 GMT
Hello, this could be interpreted as a tomcat user question but I think it
is better answered by developers.  I am trying to simulate a browser with
succeding hits to my web application that is hosted by Tomcat.  As of yet I
have been unable to make the java simulation program appear the same as the
browser does.  In short, I can not get Tomcat to pick up the session id I
try to pass back to it with succeding requests.  Here is the code I am
using.  I parse out the session id from the header returned with the first
request.  Then I try to pass it back to Tomcat with all subsequent
requests.

//h1 is the cookie with the jsessionid in it.
//the data arrays that I pass in are, this is an intranet server and will
not work for others.

            ArrayList[] m_URL =
               {
                   new ArrayList(4),
                   new ArrayList(4),
                   new ArrayList(4)
               };

            m_URL[0].add("http");
            m_URL[0].add("lnmmfs02.launch-now.com");
            m_URL[0].add(new Integer(8097));
            m_URL[0].add("/lnie/launchnow");

            m_URL[1].add("http");m_URL[1].add
("lnmmfs02.launch-now.com");m_URL[1].add(new Integer(8097));m_URL[1].add
("/lnie/launchnow/Quickstart/enterQuickstart");

            m_URL[2].add("http");m_URL[2].add
("lnmmfs02.launch-now.com");m_URL[2].add(new Integer(8097));m_URL[2].add
("/lnie/launchnow/CSR/submitCSR");

            //Parameter Array
            String[] m_parameters =
               {
                   "Function=MyHome.jsp&userid=zzwillk&password=a",
                   "",
                   "map:body.csrquery.ecckt=&map:body.csrquery.atn1=234"
               };

            //Request type array
            String[] m_requestType =
               {
                   "POST",
                   "GET",
                   "POST"
               };



import java.net.*;
import java.io.*;
import java.util.*;

public class ListURLSubmitter {

      private java.lang.String[] m_parameters = null;
      private java.lang.String[] m_requestType = null;
      private ArrayList[] m_URLList = null;
      private StringBuffer m_response = new StringBuffer();


      public ListURLSubmitter(ArrayList[] url,java.lang.String[]
parameters, java.lang.String[] type) {

            m_URLList = url;
            m_parameters = parameters;
            m_requestType = type;

      }

      public void submit() {

         try{

            //variables
            HttpURLConnection http = null;
            OutputStream buffered = null;
            OutputStreamWriter out = null;
            Reader reader = null;
            String h1 = null;

            for ( int i=0; i < m_URLList.length; i++)
            {

                URL temp = new URL((String)m_URLList[i].get(0),
(String)m_URLList[i].get(1), ((Integer)m_URLList[i].get(2)).intValue(),
(String)m_URLList[i].get(3));

                http = (HttpURLConnection)temp.openConnection();
                http.setRequestProperty("Accept", "Accept: image/gif,
image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint,
Application/vnd.ms-excel, application/msword, */*");
                http.setRequestProperty("Referer",
"http://lnmmfs02.launch-now.com:8997/lnie/Login.jsp");
                http.setRequestProperty("Accept-Language", "en-us");
                http.setRequestProperty("Accept-Encoding", "gzip,
deflate");
                    http.setRequestProperty("User-Agent", "Mozilla/4.0
(compatible; MSIE 5.5; Windows NT 5.0; ACIE55)");
                    http.setRequestProperty
("Host","lnmmfs02.launch-now.com:8097");
                    http.setRequestProperty("Connection","Keep-Alive");

//here is where I set the cookie into the request
                if (h1 != null)
                {
                    http.setRequestProperty("Cookie", h1);

                    Map map = http.getRequestProperties();
                    System.out.println(map.entrySet());
                }

                if (m_requestType[i].equals("POST")) {

                    http.setDoOutput(true);
                    http.setAllowUserInteraction(true);
                    buffered = new
BufferedOutputStream(http.getOutputStream());
                    out = new OutputStreamWriter(buffered, "8859_1");
//i tried passing it as a parameter also
                    //out = new OutputStreamWriter(buffered);
                    //out.write("JSESSIONID="+h1);//+"&"+m_parameters[i]);
                    out.write(m_parameters[i]);
                    http.connect();
                    out.flush();
                    out.close();

                } else {
                    http.setDoOutput(false);
                    http.connect();
                }

                //gives the ability to see the html response from the
server
                reader = new InputStreamReader(new
BufferedInputStream(http.getInputStream()));
                int c = 0;
                while ( (c = reader.read()) != -1 )
                {
                    m_response.append((char) c);
                    //System.out.println((char) c);
                }

 //here is where I parse it from the header!
                if (h1 == null) {
                    System.out.println();
                    System.out.println("Parsing headerfields of response");
                    int g = 0;
                    System.out.println();
                    while (http.getHeaderField(g) != null) {
                        System.out.println(http.getHeaderField(g));
                        g++;
                    }
                    System.out.println();
                    h1 = http.getHeaderField(3);

                    StringBuffer s = new StringBuffer(h1);
                    h1 = s.substring(11, 21);
                    //their version
                    h1 = http.getHeaderField("Set-Cookie");
                    int index = h1.indexOf(";");
                    if(index >= 0) h1 = h1.substring(0, index);
                    System.out.println("cookie monster: "+h1);
                }

                http.disconnect();
                reader.close();

                //System.out.println("Session ID print "
+http.getHeaderField(h1));
            }

        } catch (IOException e)
        {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }

      }

      public String getResponse() {

           return m_response.toString();

      }
}


This message is for the designated recipient only and may contain
privileged or confidential information.  If you have received it in error,
please notify the sender immediately and delete the original.  Any other
use of the email by you is prohibited.


Mime
View raw message