如何使用 HttpClient 获取 Cookie

发布于 2024-08-26 22:33:04 字数 1521 浏览 7 评论 0原文

您好,我正在使用 HttpClient 获取 Cookie,但我找不到任何 Cookie。我的代码如下:

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]);
    }
}

请告诉我我犯了一个错误。 提前致谢

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 tell me I've made a mistake.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-09-02 22:33:04

我尝试了所有可用的 CookiePolicy 并设置了“用户代理”内容,但没有成功。
从我对 java - verbose 的观察来看, getCookie() 方法中存在一个捕获异常,该异常会引发:

org.apache.commons.httpclient.util.DateParseException

可能从站点收到的 cookie (我可以使用 Firefox 上的 Cookie 编辑器看到它)有一些对 httpclient 不利的东西;查看 线程同样的问题。
据我所知,您需要启用日志记录并检查 httpclient 对 cookie 解析的作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文