You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dongsheng Song <do...@gmail.com> on 2004/11/26 07:30:32 UTC

How to clear cookies before connect server with httpclient 3.0-dev?

Whey HttpState only has addCookie/purgeExpiredCookies method, but no
purgeAllCookies mothod ?

How to remove all cookies in the code ?

        HostConfiguration config = new HostConfiguration();
        config.setProxy(record.host, record.port);        

        HttpConnectionManagerParams hcp = new HttpConnectionManagerParams();
        hcp.setConnectionTimeout(TIMEOUT_CONNECT);
        hcp.setSoTimeout(TIMEOUT_READ);        
        SimpleHttpConnectionManager hcm = new SimpleHttpConnectionManager();
        hcm.setParams(hcp);
        
        HttpClient hc = new HttpClient();
        hc.setHostConfiguration(config);
        hc.setHttpConnectionManager(hcm);
        
        HttpMethod method = new GetMethod(url);        
        int statusCode = hc.executeMethod(config, method, new HttpState());
        if (statusCode != 200) {
            throw new ConnectException("GET '" + url + "' fail");
        }

        InputStream is = method.getResponseBodyAsStream();
        if (is == null) {
            throw new IOException("No response body");
        }

        int nb = 0, rb = 0;
        byte[] buf = new byte[65536];
        while ((rb = is.read(buf)) >= 0) {
            nb += rb;
        }
        is.close();
        method.releaseConnection();
        hcm.closeIdleConnections(0);

Thanks for some help

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


[HttpClient] Re: How to clear cookies before connect server with httpclient 3.0-dev?

Posted by Michael Becke <be...@u.washington.edu>.
Hi Dongsheng,

In your sample you are creating a new instance of HttpState when 
executing the method.  Since new instances have no cookies there are 
none to remove.  You can also disable cookies by using the ignore 
cookies policy:

method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES)

Removing all cookies can also be done by calling purge with the date 
parameter:

client.getState().purgeCookies(new Date(Long.MAX_VALUE));

Even though there are these options there should also be a method on 
HttpState that removes all cookies.  I will add an enhancement request 
for this.

Mike

Dongsheng Song wrote:
> Whey HttpState only has addCookie/purgeExpiredCookies method, but no
> purgeAllCookies mothod ?
> 
> How to remove all cookies in the code ?
> 
>         HostConfiguration config = new HostConfiguration();
>         config.setProxy(record.host, record.port);        
> 
>         HttpConnectionManagerParams hcp = new HttpConnectionManagerParams();
>         hcp.setConnectionTimeout(TIMEOUT_CONNECT);
>         hcp.setSoTimeout(TIMEOUT_READ);        
>         SimpleHttpConnectionManager hcm = new SimpleHttpConnectionManager();
>         hcm.setParams(hcp);
>         
>         HttpClient hc = new HttpClient();
>         hc.setHostConfiguration(config);
>         hc.setHttpConnectionManager(hcm);
>         
>         HttpMethod method = new GetMethod(url);        
>         int statusCode = hc.executeMethod(config, method, new HttpState());
>         if (statusCode != 200) {
>             throw new ConnectException("GET '" + url + "' fail");
>         }
> 
>         InputStream is = method.getResponseBodyAsStream();
>         if (is == null) {
>             throw new IOException("No response body");
>         }
> 
>         int nb = 0, rb = 0;
>         byte[] buf = new byte[65536];
>         while ((rb = is.read(buf)) >= 0) {
>             nb += rb;
>         }
>         is.close();
>         method.releaseConnection();
>         hcm.closeIdleConnections(0);
> 
> Thanks for some help
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 

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