hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thom Hehl <t...@nowhereatall.com>
Subject Cookie Policy
Date Wed, 07 Sep 2005 12:14:29 GMT
OK, I think I'm beginning to understand what's going on with my cookie 
problem, but need some more guidance if anyone can help.

Here is my sourcecode:

    /**
     * Constructor
     * @param base the base URL from which all CGI scripts will be relative
     * @param user user name to use for logging in
     * @param passwd password to use for logging in
     */
    public ItoolsRepublish(String base, String user, String passwd)
    {
        MyLog.setLevel(Level.DEBUG);
        MyLog.info("Constructor");
        if(MyLog.isDebugEnabled())
        {
            MyLog.debug("base:"+base);
            MyLog.debug("user:"+user);
            MyLog.debug("passwd:"+passwd);
        }
        //create a singular HttpClient object
        Client = new HttpClient();

        //establish a connection within 5 seconds
        Client.getHttpConnectionManager().
            getParams().setConnectionTimeout(TIMEOUT);
        Client.getHostConfiguration().setHost(base, 80, "http");
        Client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

        StringBuffer sb=new StringBuffer("http://");
        sb.append(base);
        sb.append(ADMIN);

        UrlBase=sb.toString();
        User=user;
        Password=passwd;
    }

    /**
     * process the admin script to re-publish the web pages
     * @throws heavyweight.Exception for a variety of problems
     */
    public void process()
        throws heavyweight.Exception,
               java.io.IOException
    {
        String response;

        // Prepare login parameters
        NameValuePair userid   = new NameValuePair("id", User);
        NameValuePair password = new NameValuePair("pw", Password);

        //and login
        response=execute(UrlBase, new NameValuePair[] {userid, password});
        response=execute(UrlBase, new NameValuePair[] {
                            new NameValuePair("setup_options_edit","1")});

    }

    /**
     * execute a method
     * @param url the url to be executed
     * @param parms a name value pair array of parameters to pass to the
     *              script.
     * @return a string containing the HTML returned
     * @throws heavyweight.Exception for a variety of problems
     */
    private String execute(String url, NameValuePair[] parms)
        throws heavyweight.Exception
    {
        PostMethod method = new PostMethod(url);

        if(parms==null)
        {
            throw new IllegalArgumentException("Must pass paramters to 
execute (url, parms). For no parms, use execute(String)\n");
        }
        method.setRequestBody(parms);

        //execute the method
        String responseBody = null;
        int tries=0;
        boolean successful=false;

        do
        {
            try
            {
                Client.executeMethod(method);
                responseBody = method.getResponseBodyAsString();
                successful=true;
            } catch (HttpException he)
            {
                StringBuffer sb=new StringBuffer("Http error connecting 
to '");
                sb.append(url);
                sb.append("'\n");
                MyLog.error(sb.toString());
                MyLog.error(he.getMessage());

                if(++tries>RETRY_COUNT)
                {
                    throw new heavyweight.Exception(sb.toString(), he);
                }
            } catch (IOException ioe)
            {
                StringBuffer sb=new StringBuffer("Unable to connect to '");
                sb.append(url);
                sb.append("'\n");
                MyLog.error(sb.toString());
                MyLog.error(ioe.getMessage());

                if(++tries>RETRY_COUNT)
                {
                    throw new heavyweight.Exception(sb.toString(), ioe);
                }
            }
        } while(!successful);

        //write out the request headers
        System.out.println("*** Request ***");
        System.out.println("Request Path: " + method.getPath());
        System.out.println("Request Query: " + method.getQueryString());
        Header[] requestHeaders = method.getRequestHeaders();
        for (int i=0; i<requestHeaders.length; i++){
            System.out.print(requestHeaders[i]);
        }

        //write out the cookies
        if(MyLog.isDebugEnabled())
        {
            MyLog.debug("*** Cookies ***");
            for(Cookie ck:Client.getState().getCookies())
            {
                MyLog.debug(ck.toString());
            }
        }

        //write out the response headers
        System.out.println("*** Response ***");
        System.out.println("Status Line: " + method.getStatusLine());
        Header[] responseHeaders = method.getResponseHeaders();
        for (int i=0; i<responseHeaders.length; i++){
            System.out.print(responseHeaders[i]);
        }

        //write out the response body
        System.out.println("*** Response Body ***");
        System.out.println(responseBody);

        //clean up the connection resources
        method.releaseConnection();

        return responseBody;
    }

Now the problem appears to be that my cookie doesn't make it from the 
first request for login into the second request to do something. I've 
been researching this and have maybe figured something out.

As you can see in my source, I am using CookiePolicy.RFC_2109. I pulled 
the spec for this from http://www.ietf.org/rfc/rfc2109.txt which says:

   Version=version
      Required.  The Version attribute, a decimal integer, identifies to
      which version of the state management specification the cookie
      conforms.  For this specification, Version=1 applies.






But in my request header, when it sends the cookies back, it says:
Cookie: $Version=0; id=bmedia
Cookie: $Version=0; pw=solutions
Cookie: $Version=0; listing_search=
Cookie: $Version=0; listing_keyword=
Cookie: $Version=0; listing_pagenum=1

According to RFC_2109, shouldn't this be $Version=1? Could this be 
causing me to lose my login information on following attempts to access 
the web page?

Please advise.

Thanks.



-- 
"In every revolution, there is one man with a vision."--Jerome Bixby

Thom Hehl
<A href="www.heavyweightsoftware.com"> www.heavyweightsoftware.com</A>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message