简体   繁体   English

Java:使用 Jsoup 登录网站

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

First let me point out that I'm very new to programming so if I'm missing something obvious, I'm sorry.首先让我指出,我对编程很陌生,所以如果我遗漏了一些明显的东西,我很抱歉。

I'm trying to write a program that lets you log in to this website but I'm having trouble figuring out how to input my own strings to the email and password fields.我正在尝试编写一个程序,让您登录到这个网站,但我无法弄清楚如何将我自己的字符串输入到电子邮件和密码字段中。 I'm also not quite sure on how to check if the login went through or not...我也不太确定如何检查登录是否通过...

This is my code so far:到目前为止,这是我的代码:

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?
}

It's similar to a lot of the examples I've seen but it doesn't seem to work...它类似于我见过的很多例子,但它似乎不起作用......

For a site that supports HTTP basic access authentication you can access any page by sending the Authorization header in a request.对于支持 HTTP 基本访问身份验证的站点,您可以通过在请求中发送 Authorization 标头来访问任何页面。 Your site supports it and code could be following to access account page:您的站点支持它,并且可以通过以下代码访问帐户页面:

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