当前位置: 首页 > 面试题库 >

登录网站?(Jsoup)

贺浩漫
2023-03-14
问题内容

使用Jsoup登录网站需要什么?我相信我的代码是正确的,但是我从未使用Jsoup成功登录到网站,因此我可能会丢失一些东西。这是我的代码:

 try {                                            
        String url = ("http://quadrigacx.com/login");


        Connection.Response loginForm = (Connection.Response)Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .execute();





        Document loginDoc = loginForm.parse();


        Elements loginElements = loginDoc.select("input:not([name=client_id]):not([name=password])");

        int i = 0;

        String v[] = new String[loginElements.size()];

        for (Element element: loginElements){
            v[i++] = element.attr("value");
        }


        int ii = 0;

        String n[] = new String[loginElements.size()];

        for (Element element: loginElements){
            n[ii++] = element.attr("name");
        }


         Connection.Response loginFormLogin = (Connection.Response)Jsoup.connect(url)
                 .cookies(loginForm.cookies())          
                .data(n[0],v[0])
                 .data("client_id", "xxxxxxx")
                 .data("password", "xxxxxx")
                .data(n[1],v[1])
                .data(n[2],v[2])
                 .method(Connection.Method.POST)
                 .execute();


         Document document2 = Jsoup.connect("http://quadrigacx.com/settings")
            .cookies(loginFormLogin.cookies())
            .get();
        System.out.print(document2.toString());
    } catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

document2返回一个显示未成功登录的登录页面。有一个名为的输入值time,我认为这可能是为什么它不起作用的原因。这个值会随着时间的推移而上升。我运行了两次代码,时间变量返回了15112268601511226876。我的代码需要大约10秒钟的时间来打印文档,因此,time变量在发送发布请求时是否已经更改?我不确定这是否是问题。也许还有其他我没看到的东西?谢谢。

编辑:这是代码,我已经使用用户ID和密码登录后发布了身份验证。loginCookies是第一次登录时的Cookie。
Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", googleCode.getText()) .data("email_code", emailCode.getText()) .data("authenticate", "Authenticate") .followRedirects(true) .execute();
我也尝试过: byte[] gcText = googleCode.getText().getBytes(ISO_8859_1); String gcValue = new String(gcText, UTF_8); byte[] ecText = emailCode.getText().getBytes(ISO_8859_1); String ecValue = new String(ecText, UTF_8); Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", gcValue) .data("email_code", ecValue) .data("authenticate", "Authenticate") .followRedirects(true) .execute();


问题答案:

您需要在请求中添加更多参数: csrf,时间,哈希

空表代码:

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class Quadrigacx {
    public static String[] TABLE = new String[]{}; // Add data here

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {

        final String URL = "https://www.quadrigacx.com/login/";
        String password = "password";
        String clientId = "id";

        String hashPassword = getHash(new String[]{clientId, password});

        Connection.Response response = Jsoup.connect(URL)
                .userAgent("Mozilla")
                .method(Connection.Method.GET)
                .execute();

        Element csrf = response.parse().select("input[name=csrf]").first();
        Element time = response.parse().select("input[name=time]").first();
        Element hash = response.parse().select("input[name=hash]").first();

        Jsoup.connect(URL)
                .userAgent("Mozilla")
                .method(Connection.Method.POST)
                .cookies(response.cookies())
                .data("password", hashPassword)
                .data("client_id", clientId)
                .data("csrf", csrf.attr("value"))
                .data("time", time.attr("value"))
                .data("hash", hash.attr("value"))
                .execute();

        String googleCode = "";

        while (!googleCode.matches("^(?=[0-9]+)\\d{6}$")) {
            System.out.print("Please enter the Two-Factor Authentication to validate your login. (Numbers Only): ");
            Scanner in = new Scanner(System.in);
            googleCode = in.nextLine();
        }

        Jsoup.connect("https://www.quadrigacx.com/authenticate")
                .userAgent("Mozilla")
                .method(Connection.Method.POST)
                .cookies(response.cookies())
                .data("google_code", googleCode)
                .data("redirect", "dash")
                .data("authenticate", "Authenticate")
                .execute();

        response = Jsoup.connect("https://www.quadrigacx.com/dash/")
                .userAgent("Mozilla")
                .cookies(response.cookies())
                .method(Connection.Method.GET)
                .execute();

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

    }

    private static String getHash(String[] loginArray) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        StringBuilder h = new StringBuilder();
        for (String data : loginArray)
            h.append(data).append(getSalt(data));

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] byteHash = digest.digest(h.toString().getBytes(StandardCharsets.UTF_8));

        StringBuilder sb = new StringBuilder(byteHash.length * 2);
        for (byte b : byteHash)
            sb.append(String.format("%02x", b));

        return sb.toString();
    }

    private static String getSalt(String arg) throws UnsupportedEncodingException {
        return TABLE[getLastByte(arg)];
    }

    private static int getLastByte(String login) throws UnsupportedEncodingException {
        final byte[] utf8Bytes = login.getBytes("UTF-8");
        return utf8Bytes[utf8Bytes.length - 1];
    }
}

完整的工作代码,您可以在这里找到(很长):

https://pastebin.com/aBf1M3fX



 类似资料:
  • 问题内容: 在此网站上,您可以输入您的学生卡号,然后显示该卡上还剩多少钱。我想使用JSOUP获得信息。这是我当前的代码,但是不起作用, 我没有太多经验,所以我不知道在哪里寻找问题。一些想法: 我应该使用还是? 在使用chrome devoloper工具时,发布数据是我使用功能发送的所有数据。但是,如果发送时出现错误,为什么? 我应该发送解密或加密的数据吗?(两者均在chrome devoloper

  • 我正在尝试使用jsoup登录一个网站,我很确定我正在解析所有需要解析的东西,我只是不知道出了什么问题。 我用这个做参考:http://cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf 以下是我的AsycntTask doInBackground中的代码: 但问题是,当我登录时,它不包含页面的文档,它包含一个错误页面的文档,该页面只显示

  • 我试图登录使用领英J汤,我已经尝试了几种方法,但总是遇到错误403,我哪里出错了? 测试一 错误控制台 组织。jsoup。HttpStatusException:获取URL时发生HTTP错误。状态=403,URL=https://www.linkedin.com在org。jsoup。帮手HttpConnection$响应。在org上执行(HttpConnection.java:590)。jsoup

  • 问题内容: 我正在尝试使用Python登录到此页面。 我尝试使用另一篇文章中描述的步骤,并获得了以下代码: 但这给了我以下输出: 我究竟做错了什么? 问题答案: 我建议使用精彩的模块。 下面的代码将使您登录到该站点,并在会话期间将cookie保留下来。

  • 问题内容: 我想用Java登录到一个网站。我使用的是org.apache.http,我已经写了 尽管我仍然无法登录,但它正确发送了我测试过的帖子表格。我要登录的网站是http://www.xtratime.org/forum/ 关于此的任何想法或是否有其他方法? 问题答案: 在提交页面之前,请对密码进行编码()。您应该在代码中执行相同的操作。 该属性的值与您的代码()不匹配。您应该将发布请求发送到

  • 问题内容: 我正在尝试从此站点http://cheese.formice.com/maps/@5865339获取信息,但是当我请求使用urllib.urlopen时,它说我需要登录,我正在使用此代码: 我做错了什么? 问题答案: 它不是直接使用,但是您可能会发现使用该包更容易。有一个对象看到这个答案 这将使您登录到该站点。您可以通过以下方式进行验证: 登录后,您可以调用所需的特定网址。