如何使用Jsoup登录HTTPS网站? [英] How to log in to an HTTPS website with Jsoup?

查看:127
本文介绍了如何使用Jsoup登录HTTPS网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近对webcrawlers感兴趣并决定尝试Jsoup。我不确定如何使用它登录网站。我看到了另外一篇关于它的SO文章,但无法拼凑出如何做到这一点。

I've been interested in webcrawlers recently and decided to try Jsoup. I'm not exactly sure how to log into a website with it though. I saw another SO post about it but couldn't piece together how to do it.

我一直在努力寻找网站www.tickld.com和登录网站是 https://www.tickld.com/signin

I've been trying to crawl around with a site www.tickld.com and the login site is "https://www.tickld.com/signin".

我不确定我是否正确使用Jsoup(我确定这是主要原因),如果错误是.jks,或者我是输入错误的信息,我真的不知道如何测试代码的哪一部分失败。

I'm not sure if I'm using Jsoup correctly(I'm certain this is the main reason), if the error is the .jks, or if I'm entering the wrong information, and I don't really see how to test which part of the code is failing.

        System.setProperty("javax.net.ssl.trustStore", "filePath\\keystore.jks");

        Connection.Response loginForm = Jsoup.connect("https://www.tickld.com/signin")
                .method(Connection.Method.GET).execute();

        Document document = Jsoup.connect("https://www.tickld.com/signing")
                .data("l_username", "myUsername")
                .data("l_password", "myPassword")
                .cookies(loginForm.cookies())
                .post();

但无论我在做什么,它都没有登录到网站,只是带我去登录页面。

but whatever I'm doing, it is not logging into the site, it is only taking me to the signin page.

推荐答案

登录由ajax处理。我正在使用chrome,所以这就是我所做的。
尝试从浏览器通过表单登录。按F12,然后按控制台。
您将看到类似这样的内容 XHR已完成加载:POSThttps://www.tickld.com/ajax/login.php。。当您发出POST请求时,将其设置为放置在表单标记的操作参数中的URL 。
在这种情况下,不存在这样的url,因为它是由javascript处理的。

The signing in is handled by ajax. I'm using chrome, so this is what I did. Try to login via the form from a browser. Press F12 and then press Console. You will see something like this XHR finished loading: POST "https://www.tickld.com/ajax/login.php". . When you make the POST request, you make it to the url that is placed in the action parameter of the form tag. In this case, no such url exists, because it is handled by javascript.

试试这个并查看它是否有效。

Try this and see if it works.

Document document = Jsoup.connect("https://www.tickld.com/ajax/login.php")
                .data("l_username", "myUsername")
                .data("l_password", "myPassword")
                .cookies(loginForm.cookies())
                .post();

如果没有那么你可能需要使用一些无头浏览器(可以处理js执行)比如selenium webdriver。

If it doesn't then you might need to use some headless browser (which can handle js execution) like selenium webdriver.

更新

Connection.Response login = Jsoup.connect("https://www.tickld.com/signin")
                                .data("l_username", "myUsername")
                                .data("l_password", "myPassword")
                                .method(Connection.Method.POST)
                                .execute();

Document document = Jsoup.connect("http://www.tickld.com/user/chosimbaaaa")
                .cookies(login.cookies())
                .get();

这篇关于如何使用Jsoup登录HTTPS网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆