How to get Cookies using HttpClient
- by Sunil
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