Java: accesso a un sito web utilizzando Jsoup
Innanzitutto vorrei sottolineare che sono molto nuovo alla programmazione, quindi se mi manca qualcosa di ovvio, mi dispiace.
Sto cercando di scrivere un programma che ti permetta di accedere a questo sito ma ho difficoltà a capire come inserire le mie stringhe nei campi e-mail e password. Anche io non sono del tutto sicuro su come verificare se il login è andato attraverso o no...
Questo è il mio codice finora:
public static void main(String[] args) throws Exception {
String loginURL = "https://www.skanetrafiken.se/inloggning?ReturnUrl=%2fmitt-konto%2fse-saldo-och-ladda-kort%2f"; // URL of the login page
String accountURL = "https://www.skanetrafiken.se/mitt-konto/se-saldo-och-ladda-kort/"; // The URL you get to after successfully logging in
Document res = Jsoup
.connect(loginURL)
.data("loginInputModel.Email", "[email protected]") //Not sure if these are the correct values to be changed or if this even changes them
.data("loginInputModel.Password", "myPassword")
.post();
System.out.println(res); // What should be printed to check to see if it worked?
}
È simile a molti degli esempi che ho visto ma non sembra lavoro...
4
Author: HyvelTjuven, 2016-02-21
1 answers
Per un sito che supporta l'autenticazione HTTP basic access è possibile accedere a qualsiasi pagina inviando l'intestazione di autorizzazione in una richiesta. Il tuo sito lo supporta e il codice potrebbe essere seguito per accedere alla pagina dell'account:
import java.io.IOException;
import java.util.Map;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
public static void main(String[] args) throws IOException {
// We need run initial request to obtain RequestVerificationToken
String initialURL = "https://www.skanetrafiken.se/inloggning";
Document doc = Jsoup
.connect(initialURL)
.get();
String requestVerificationToken = doc.select("input[name=__RequestVerificationToken]").get(0).val();
// Do login (all headers and more important all form fields should be populated)
String loginURL = "https://www.skanetrafiken.se/inloggning/LoginPost/";
Response res = Jsoup.connect(loginURL)
.header("Accept", "*/*")
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.header("Origin", "https://www.skanetrafiken.se")
.header("X-Requested-With", "XMLHttpRequest")
.header("Referer", "https://www.skanetrafiken.se/inloggning")
.data("__RequestVerificationToken", requestVerificationToken)
.data("loginInputModel.ReturnUrl", "")
.data("loginInputModel.Role", "Private")
.data("loginInputModel.Email", "<email>")
.data("loginInputModel.Password", "<password>")
.data("X-Requested-With", "XMLHttpRequest")
.userAgent("Mozilla/5.0")
.ignoreContentType(true)
.method(Method.POST)
.execute();
// Keep logged in (store cookies for next calls)
Map<String, String> cookies = res.cookies();
// Request a desired page
String accountURL = "https://www.skanetrafiken.se/mitt-konto/se-saldo-och-ladda-kort/";
Document doc2 = Jsoup
.connect(accountURL)
.cookies(cookies)
.get();
// Work with the doc
System.out.println(doc2);
}
}
1
Author: Mike Shauneu, 2016-02-21 13:38:29