簡體   English   中英

Java:使用 Jsoup 登錄網站

[英]Java: Logging in to a website using Jsoup

首先讓我指出,我對編程很陌生,所以如果我遺漏了一些明顯的東西,我很抱歉。

我正在嘗試編寫一個程序,讓您登錄到這個網站,但我無法弄清楚如何將我自己的字符串輸入到電子郵件和密碼字段中。 我也不太確定如何檢查登錄是否通過...

到目前為止,這是我的代碼:

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", "myEmail@email.com") //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?
}

它類似於我見過的很多例子,但它似乎不起作用......

對於支持 HTTP 基本訪問身份驗證的站點,您可以通過在請求中發送 Authorization 標頭來訪問任何頁面。 您的站點支持它,並且可以通過以下代碼訪問帳戶頁面:

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM