开发者

How to get Cookies using HttpClient

开发者 https://www.devze.com 2022-12-24 22:26 出处:网络
Hello I am using HttpClient to get Cookies but I am unable find any cookies. My Code is given below: public class LoginTab {

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.getHttpConnectionMan开发者_如何学JAVAager().
                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 tell me I've made a mistake. Thanks in advance


I have tried with all the CookiePolicy available and also setting "user-agent" stuff without success. From what i see with java - verbose there's a trapped exception in getCookie() method that raises:

org.apache.commons.httpclient.util.DateParseException

Probably cookie received from site (i can see it with Cookie Editor on firefox) has something not good for httpclient; have a look at this thread for the same problem.
From what i see, you need to enable logging and check what httpclient does on cookie parsing.

0

精彩评论

暂无评论...
验证码 换一张
取 消