有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用jsoup维护变量cookie和会话?

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更改以及维护会话

我正在为我的simple machines论坛编写一个应用程序,当你点击一些自定义行为时,它会更改cookie配置

但是,如果这个应用程序对我的网站很好的话,我会发布一个版本供其他人在其他论坛上使用

我知道我正朝着正确的方向前进,但逻辑是踢我的屁股

如有任何建议,将不胜感激


共 (2) 个答案

  1. # 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);
    // ...
    

    请注意,即将发布的JSoup1.6.2将附带一个新的Connection#cookies(Map)方法,这将使for循环每次都变得多余

  2. # 2 楼答案

    巴卢斯克+1

    我对你们的代码做了一些修改,它对我很有用,所以你们可以从网站上获取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();
    }