如何用jsoup设置jsessionid cookie?
How to set the jsessionid cookie with jsoup?
我对以下用于大学网站身份验证的测试方法有疑问。
我正在使用 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();
}
}
您可以检索 cookie,创建对网站主页的另一个请求。
package com.github.davidepastore.Whosebug38768839;
import java.io.IOException;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
/**
* Whosebug 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();
}
}
}
我对以下用于大学网站身份验证的测试方法有疑问。 我正在使用 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();
}
}
您可以检索 cookie,创建对网站主页的另一个请求。
package com.github.davidepastore.Whosebug38768839;
import java.io.IOException;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
/**
* Whosebug 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();
}
}
}