【问题标题】:Is there any way to use firefox's cookies with the Apache HttpClient class?有没有办法在 Apache HttpClient 类中使用 firefox 的 cookie?
【发布时间】:2012-05-19 02:06:57
【问题描述】:

我显然对 cookie 知之甚少,它们是否兼容等等等等。如果可能的话,有人可以举个例子吗?

【问题讨论】:

    标签: java cookies apache-commons-httpclient


    【解决方案1】:

    我不太清楚您所说的“使用 Firefox cookie”是什么意思。这是我编写的一段代码,用于通过将 cookie 与 Apache HttpClient 请求一起传递来从 Goolge Reader API 中获取数据。

    它需要发送一个 Google 在连接时提供给您的 cookie(“SID”)及其请求。这是你要找的吗?

    // Obtain SID
        GetMethod getLogin = new GetMethod("https://www.google.com/accounts/ClientLogin?service=reader&Email="+
                login+"&Passwd="+password);
        client.executeMethod(getLogin);
        String loginText = getLogin.getResponseBodyAsString();
        // The value of SID is loginText.substring (4,firstLineLength)
        BufferedReader reader = new BufferedReader(new StringReader(loginText));
        String SID = loginText.substring(4, reader.readLine().length() );
    
    GetMethod grabData = new GetMethod("http://www.google.com/reader/atom/user/-/state/com.google/broadcast");
    grabData.setRequestHeader("Cookie", "SID="+SID);
    client.executeMethod(grabData);
    

    【讨论】:

    • 嗯,那么 cookie 是否只是字符串数据?
    • 他们在这个特定的例子中。我正在向 Google 发送带有登录信息的“获取”请求,并将 Google 的响应(恰好包含我需要的 cookie)存储为响应文本的子字符串。不过,就从本地计算机获取 cookie 而言,恐怕我不确定 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2013-06-24
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多