【问题标题】:How to set the jsessionid cookie with jsoup?如何使用 jsoup 设置 jsessionid cookie?
【发布时间】:2016-12-10 15:58:07
【问题描述】:

我对以下用于对大学网站进行身份验证的测试方法有疑问。 我正在使用 Jsoup,但无法让 cookie (jsessionid) 登录。 方法是这样的:

public static void Authentication() {

    String strURL = "https://www.studenti.ict.uniba.it/esse3/auth/Logon.do";
    String strUserId = "prova";
    String strPasword = "prova";

    String authString = strUserId + ":" + strPasword;

    String encodedString = new String( Base64.encodeBase64(authString.getBytes()) );

    try{

        Response response = Jsoup.connect(strURL)
                .header("Authorization", "Basic " + encodedString)
                .method(org.jsoup.Connection.Method.GET)
                .timeout(30000)
                .execute();

        System.out.println(response.parse());

        System.out.println("Autenticato");

    }catch (IOException e){
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java cookies jsoup jsessionid


    【解决方案1】:

    您可以检索 cookie,创建对网站主页的另一个请求。

    package com.github.davidepastore.stackoverflow38768839;
    
    import java.io.IOException;
    
    import org.jsoup.Connection.Response;
    import org.jsoup.Jsoup;
    
    /**
     * Stackoverflow 38768839 answer.
     *
     */
    public class App {
    
        private static final String COOKIE_NAME = "JSESSIONID";
    
        public static void main(String[] args) throws IOException {
            // Step 1 - Get the cookie
            String homeURL = "https://www.studenti.ict.uniba.it/esse3/Home.do";
            Response response = Jsoup.connect(homeURL).execute();
            String jsessionid = response.cookie(COOKIE_NAME);
            System.out.println(COOKIE_NAME + " cookie: " + jsessionid);
    
            // Step 2 - Try to login
            String strURL = "https://www.studenti.ict.uniba.it/esse3/auth/Logon.do";
            String strUserId = "prova";
            String strPasword = "prova";
    
            String authString = strUserId + ":" + strPasword;
    
            String encodedString = new String(Base64.encodeBase64(authString
                    .getBytes()));
    
            try {
    
                response = Jsoup.connect(strURL)
                        .header("Authorization", "Basic " + encodedString)
                        .cookie(COOKIE_NAME, jsessionid)
                        .method(org.jsoup.Connection.Method.GET).timeout(30000)
                        .execute();
    
                System.out.println(response.parse());
    
                System.out.println("Autenticato");
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 谢谢。对 cookie 的帮助非常好。现在的问题是它没有登录并给我错误 401,尽管访问凭据是正确的。错误在“执行”函数中。
    • @VincEnzo 问题是,如果我没有凭据,我就无法测试它。
    • 您可以留下您的电子邮件吗?您发送登录凭据
    猜你喜欢
    • 2012-09-26
    • 2021-07-22
    • 2015-01-09
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2019-05-14
    • 2017-11-24
    相关资源
    最近更新 更多