How to get Cookies using HttpClient

Posted by Sunil on Stack Overflow See other posts from Stack Overflow or by Sunil
Published on 2010-03-30T12:25:05Z Indexed on 2010/03/30 12:43 UTC
Read the original article Hit count: 724

Filed under:
|

Hello I am using HttpClient to get Cookies but I am unable find any cookies.My Code is given below public class LoginTab {

private Cookie[] cookies;
HttpClient httpClient;
HttpState httpState;
HashMap postData;

public LoginTab() {
    httpClient = new HttpClient();
    httpState = new HttpState();
    httpClient.getHttpConnectionManager().
            getParams().setConnectionTimeout(300000);
    httpClient.setState(httpState);
    // RFC 2101 cookie management spec is used per default
    // to parse, validate, format & match cookies
    httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    postData= new HashMap();
}

public String getMethod(String url) {
    GetMethod getMethod = new GetMethod(url);
      String pageSoure="";
    try{
    httpClient.executeMethod(getMethod);
     pageSoure=getMethod.getResponseBodyAsString();
    extractUsefulPostData(pageSoure, postData);
    getMethod.releaseConnection();
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return pageSoure;
}
public static void main(String[]arg)
{
    LoginTab loginTab= new LoginTab();
    System.out.println(loginTab.getMethod("http://tab.com.au/"));
    Cookie [] cookies=loginTab.httpState.getCookies();
    System.out.println(cookies.length);
    for(int i=0;i<cookies.length;i++)
        System.out.println(cookies[i]);
}

}

Please suggest me where is the mistake. Thanks in advance

© Stack Overflow or respective owner

How to get Cookies using HttpClient

Posted by Sunil on Stack Overflow See other posts from Stack Overflow or by Sunil
Published on 2010-03-30T08:28:02Z Indexed on 2010/03/30 8:43 UTC
Read the original article Hit count: 724

Filed under:
|

Hello I am using HttpClient to get Cookies but I am unable find any cookies.My Code is given below

public class LoginTab {

    private Cookie[] cookies;
    HttpClient httpClient;
    HttpState httpState;
    HashMap postData;

    public LoginTab() {
        httpClient = new HttpClient();
        httpState = new HttpState();
        httpClient.getHttpConnectionManager().
                getParams().setConnectionTimeout(300000);
        httpClient.setState(httpState);
        // RFC 2101 cookie management spec is used per default
        // to parse, validate, format & match cookies
        httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
        postData= new HashMap();
    }

    public String getMethod(String url) {
        GetMethod getMethod = new GetMethod(url);
          String pageSoure="";
        try{
        httpClient.executeMethod(getMethod);
         pageSoure=getMethod.getResponseBodyAsString();
        extractUsefulPostData(pageSoure, postData);
        getMethod.releaseConnection();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return pageSoure;
    }
    public static void main(String[]arg)
    {
        LoginTab loginTab= new LoginTab();
        System.out.println(loginTab.getMethod("http://tab.com.au/"));
        Cookie [] cookies=loginTab.httpState.getCookies();
        System.out.println(cookies.length);
        for(int i=0;i<cookies.length;i++)
            System.out.println(cookies[i]);
    }
}

Please suggest me where is the mistake. Thanks in advance

© Stack Overflow or respective owner

Related posts about commons-httpclient

Related posts about java