使用Jsoup登录 [英] Login using Jsoup
问题描述
我想登录一个网站并保留cookie.使用Jsoup的登录部分出现问题;
I would like to login into a website and keep the cookies. I have issues with the login part using Jsoup;
我的代码;
Connection.Response loginForm = Jsoup.connect("URL" + "/login/").cookies(cookies).method(Connection.Method.GET).execute();
formData.put("username", "#######");
formData.put("pwd", "########");
formData.put("hidden","69");
formData.put("token", loginForm.parse().select("input#token").first().attr("value"));
Connection.Response homePage = Jsoup.connect("URL" + "/login/")
.referrer("URL" + "/login/")
.followRedirects(true)
.cookies(cookies)
.data(formData)
.method(Connection.Method.POST)
.execute();
cookies_2.putAll(homePage.cookies()); // save the cookies, this will be passed on to next request
如果我进入登录页面并使用开发人员工具,则有;
If i go to the login page and use the developper tool there are ;
编辑1;
现在的问题是我获得了网站的主页,但是没有登录部分,我保留了连接或注册"按钮.
The problem now is I get homepage of the website but without the section of login, I keep the "Connect or register" button.
我输出了cookies_2,它与Chrome日志中的cookie相同;
I output the cookies_2 and it is the same that the cookies in the Chrome logs ;
现在,我不明白的是为什么,如果我拥有正确的cookie,我不会得到loggin吗?
Now, what I don't understand is why, I don't get loggin if I have the right cookies ?
编辑2;
我用最终解决方案修改了代码,它可以正常工作!
I chanded my code with the final solution and it work !
感谢您的帮助!
推荐答案
少量备注:
- 您的HTTP状态为302.使用
Jsoup.connect(...).followRedirects(true)
是个好主意
- 某些服务器会检查您来自的站点,因此建议设置引荐来源标头:
Jsoup.connect(...).referrer(URL + "/login")
- 您确定
hidden
值始终为69
吗?也许每个请求都不同.您可以这样动态获取它:formData.put("hidden",html.select("co_js").first().attr("value"));
-
我不喜欢您获取令牌的方式.让我们使用Jsoup提取它:
String authToken = html.select("input#token").first().attr("value");
- You're getting HTTP status 302. It may be a good idea to use
Jsoup.connect(...).followRedirects(true)
- Some servers check the site you're coming from so it's recommened to set referrer header:
Jsoup.connect(...).referrer(URL + "/login")
- Are you sure the
hidden
value is always69
? Maybe it's different for each request. You can get it dynamically like this:formData.put("hidden",html.select("co_js").first().attr("value"));
I don't like your way of getting the token. Let's use Jsoup to extract it:
String authToken = html.select("input#token").first().attr("value");
修改:
通过让服务器知道您可以处理压缩的页面,可以减小下载的页面大小.对于每个请求,请使用:Jsoup.connect(...).header("accept-encoding", "gzip, deflate")
它是透明的,您无需执行任何特殊处理即可,但它在内部起作用.
By letting server know you can handle compressed pages you can decrease downloaded page size. For every request use: Jsoup.connect(...).header("accept-encoding", "gzip, deflate")
It's transparent and you don't have to do anything special to handle it, but it works internally.
编辑2 :
根据先前的建议提供最终解决方案:
Providing final solution based on previous advices:
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Stackoverflow51734840 {
private static final String URL = "https://time2watch.in";
private static final String URL_LOGIN = URL + "/login/";
static String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
public static void main(final String[] args) throws Exception {
Map<String, String> headers = new HashMap<String, String>();
headers.put("accept-encoding", "gzip, deflate");
Connection.Response loginForm = Jsoup.connect(URL + "/login").userAgent(userAgent).headers(headers).execute();
Map<String, String> cookies = loginForm.cookies();
Document html = loginForm.parse();
String authToken = html.select("input#token").first().attr("value");
System.out.println("Found authToken:" + authToken);
Map<String, String> formData = new HashMap<String, String>();
formData.put("username", "!!!!!!!!!!!!!!!!!!");
formData.put("pwd", "!!!!!!!!!!!!!!!!!!");
formData.put("hidden", "69");
formData.put("token", authToken);
headers.put("Content-Type", "application/x-www-form-urlencoded");
System.out.println("cookies before login:");
System.out.println(cookies);
System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));
Connection.Response afterLoginPage = Jsoup.connect(URL_LOGIN).cookies(cookies).headers(headers)
.userAgent(userAgent).data(formData).method(Connection.Method.POST).referrer(URL_LOGIN).execute();
// update cookies
cookies = afterLoginPage.cookies();
System.out.println("cookies after login:");
System.out.println(cookies);
System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));
Response homePage = Jsoup.connect(URL).cookies(cookies).method(Connection.Method.GET).userAgent(userAgent)
.referrer(URL_LOGIN).followRedirects(true).referrer(URL_LOGIN).headers(headers).execute();
Document doc = homePage.parse();
System.out.println("Error? " + doc.text().contains("Erreur"));
System.out.println("OK? " + !doc.text().contains("Se connecter"));
System.out.println("Logged in as: " + doc.select(".dropdown-toggle").text());
}
}
这篇关于使用Jsoup登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!