【问题标题】:how to maintain variable cookies and sessions with jsoup?如何使用 jsoup 维护可变 cookie 和会话?
【发布时间】:2011-12-05 10:04:48
【问题描述】:
public boolean isGood(String path)
{
    if (p != path)
    {
        good = false;
    }

    if (good)
    {
        try 
        {
            Connection connection = Jsoup.connect(path);
            Map<String, String> cookys = Jsoup.connect(path).response().cookies();

            if (cookys != cookies)
                cookies = cookys;

            for (Entry<String, String> cookie : cookies.entrySet()) 
            {
                connection.cookie(cookie.getKey(), cookie.getValue());
            }

            Doc = connection.get();
            good = true;
        }
        catch (Exception e) 
        {
            rstring = e.getMessage().toString();
            good = false;
        }
    }
    else
    {
        try
        {
            Response response = Jsoup.connect(path).execute();
            cookies = response.cookies();
            Doc = response.parse();
            good = true;
        }
        catch (Exception e) 
        {
            rstring = e.getMessage().toString();
            good = false;
        } 
    }       
    return good;
}

这个方法不对。我想弄清楚的是一种在不知道将存在哪些 cookie 的情况下能够处理 cookie 更改以及维护会话的方法。

我正在为我的简单机器论坛编写一个应用程序,当您点击一些自定义行为时,它会更改其 cookie 配置。

但如果该应用程序在我的网站上运行良好,我将发布一个版本供其他人用于其他论坛。

我知道我正朝着正确的方向前进,但逻辑有点让我头疼。

任何建议将不胜感激。

【问题讨论】:

  • 除了 BalusC 的 cmets,p != path 的意思不太可能是您真正的意思,尽管它可能

标签: java session cookies jsoup


【解决方案1】:

这段代码非常混乱。流程不合逻辑,异常处理很糟糕。像if (p != path)if (cookys != cookies) 这样的对象引用比较毫无意义。要比较对象的内容,您需要改用equals() 方法。

说到点子上,我知道您希望在同一域上的一堆后续 Jsoup 请求中维护 cookie。在这种情况下,您需要基本上遵循以下流程:

Map<String, String> cookies = new HashMap<String, String>();

// First request.
Connection connection1 = Jsoup.connect(url1);
for (Entry<String, String> cookie : cookies.entrySet()) {
    connection1.cookie(cookie.getKey(), cookie.getValue());
}
Response response1 = connection1.execute();
cookies.putAll(response1.cookies());
Document document1 = response1.parse();
// ...

// Second request.
Connection connection2 = Jsoup.connect(url2);
for (Entry<String, String> cookie : cookies.entrySet()) {
    connection2.cookie(cookie.getKey(), cookie.getValue());
}
Response response2 = connection2.execute();
cookies.putAll(response2.cookies());
Document document2 = response2.parse();
// ...

// Third request.
Connection connection3 = Jsoup.connect(url3);
for (Entry<String, String> cookie : cookies.entrySet()) {
    connection3.cookie(cookie.getKey(), cookie.getValue());
}
Response response3 = connection3.execute();
cookies.putAll(response3.cookies());
Document document3 = response3.parse();
// ...

// Etc.

这可以重构为以下方法:

private Map<String, String> cookies = new HashMap<String, String>();

public Document get(url) throws IOException {
    Connection connection = Jsoup.connect(url);
    for (Entry<String, String> cookie : cookies.entrySet()) {
        connection.cookie(cookie.getKey(), cookie.getValue());
    }
    Response response = connection.execute();
    cookies.putAll(response.cookies());
    return response.parse();
}

可以用作

YourJsoupWrapper jsoupWrapper = new YourJsoupWrapper();

Document document1 = jsoupWrapper.get(url1);
// ...

Document document2 = jsoupWrapper.get(url2);
// ...

Document document3 = jsoupWrapper.get(url3);
// ...

请注意,即将推出的 Jsoup 1.6.2 将附带一个新的 Connection#cookies(Map) 方法,这应该使 for 循环每次都是多余的。

【讨论】:

  • 非常感谢。我搜索并搜索了正确方法的示例。这种方式绝对比我尝试的方式更好。大声笑
  • 来自未来的人!出于某种原因,我发现 JSoup 非常方便。只是我的两分钱是,在当前的 JSoup 库中,有一个名为 cookies (Map cookies) 的方法可以添加键/值对。所以上面的foreach可以替换为:connection.cookies(cookies)
  • 你可以用 connection.cookies(cookies) 代替 for 循环
  • @crl:确实,另请参阅上述答案的最后一段以及this answer 上 Jsoup 开发人员的 cmets。
【解决方案2】:

+1 为 BalusC

我在您的代码中更改了一些内容,它对我有用,因此您从站点获取 cookie,而不是获取文档

public Document get(String url) throws IOException {
    Connection connection = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    Connection.Response response = connection.execute();
    connection.cookies(response.cookies());
    return connection.get();
}

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    相关资源
    最近更新 更多