Java 登录页面自动化时返回登录页面
我正在尝试使用JSoup自动登录我的研究所网站。我试着用现在的教程 但每次我这样做时,都会返回同一网页(登录页)的元素,而不是我的仪表板的元素。似乎我无法登录,尽管登录凭据正确并且正在网站上工作 下面是我的代码Java 登录页面自动化时返回登录页面,java,redirect,login,web-crawler,jsoup,Java,Redirect,Login,Web Crawler,Jsoup,我正在尝试使用JSoup自动登录我的研究所网站。我试着用现在的教程 但每次我这样做时,都会返回同一网页(登录页)的元素,而不是我的仪表板的元素。似乎我无法登录,尽管登录凭据正确并且正在网站上工作 下面是我的代码 String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"; try
String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
try {
//grab login form page first
Response loginPageResponse =
Jsoup.connect("https://www.icloudemserp.com/corecampus/checkuser1.php")
.referrer("https://www.icloudemserp.com/corecampus/index.php/")
.userAgent(userAgent)
.timeout(10 * 1000)
.followRedirects(true)
.method(Connection.Method.GET)
.execute();
System.out.println("Fetched login page");
//get the cookies from the response, which we will post to the action URL
Map<String, String> mapLoginPageCookies = loginPageResponse.cookies();
String YOUR_USER_ID = "MREI2033";
String YOUR_PASSWORD = "student1234";
String branch_id = "9";
//lets make data map containing all the parameters and its values found in the form
Map<String, String> mapParams = new HashMap<String, String>();
mapParams.put("branchid", "1");
mapParams.put("userid", "");
mapParams.put("pass_word", "");
mapParams.put("branchid", "9");
mapParams.put("sel_acad_yr", "2013-2014");
mapParams.put("sel_sem", "Sem+1");
System.out.println("*****************************************"+YOUR_USER_ID+YOUR_PASSWORD);
//URL found in form's action attribute
String strActionURL = "https://www.icloudemserp.com/corecampus/checkuser1.php";
Response responsePostLogin = Jsoup.connect(strActionURL)
//referrer will be the login page's URL
.referrer("https://www.icloudemserp.com/corecampus/index.php/")
//user agent
.userAgent(userAgent)
//connect and read time out
.timeout(10 * 1000)
//post parameters
.data(mapParams)
//cookies received from login page
.cookies(mapLoginPageCookies)
//many websites redirects the user after login, so follow them
.followRedirects(true)
.method(Connection.Method.POST)
.execute();
System.out.println("HTTP Status Code: " + responsePostLogin.statusCode());
//parse the document from response
Document document = responsePostLogin.parse();
System.out.println(document);
//get the cookies
Map<String, String> mapLoggedInCookies = responsePostLogin.cookies();
/*
* For all the subsequent requests, you need to send
* the mapLoggedInCookies containing cookies
*/
Document doc = Jsoup.connect("https://www.icloudemserp.com/corecampus/student/student_index.php")
.cookies(mapLoggedInCookies)
.userAgent(userAgent)
.get();
System.out.println("##################################################");
System.out.println(doc);
}
String userAgent=“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/67.0.3396.99 Safari/537.36”;
试一试{
//首先抓取登录表单页面
响应登录响应=
Jsoup.connect(“https://www.icloudemserp.com/corecampus/checkuser1.php")
.推荐人(”https://www.icloudemserp.com/corecampus/index.php/")
.userAgent(userAgent)
.超时(10*1000)
.followRedirects(true)
.method(Connection.method.GET)
.execute();
System.out.println(“获取的登录页面”);
//从响应中获取cookies,我们会将其发布到操作URL
Map mapLoginPageCookies=loginPageResponse.cookies();
将您的\u USER\u ID=“MREI2033”字符串串起来;
将您的_密码设置为String=“student1234”;
字符串分支\u id=“9”;
//让我们制作包含表单中所有参数及其值的数据映射
Map mapParams=新HashMap();
mapParams.put(“branchid”,“1”);
mapParams.put(“userid”,”);
mapParams.put(“pass_word”和“”);
mapParams.put(“branchid”,“9”);
mapParams.put(“2013-2014年”);
映射参数put(“sel_sem”、“sem+1”);
System.out.println(“****************************************”+您的用户ID+您的密码);
//在表单的操作属性中找到URL
字符串strActionURL=”https://www.icloudemserp.com/corecampus/checkuser1.php";
Response responsePostLogin=Jsoup.connect(strActionURL)
//referer将是登录页面的URL
.推荐人(”https://www.icloudemserp.com/corecampus/index.php/")
//用户代理
.userAgent(userAgent)
//连接并读取超时
.超时(10*1000)
//post参数
.数据(mapParams)
//从登录页面接收的cookies
.cookies(mapLoginPageCookies)
//许多网站会在用户登录后重定向用户,所以请遵循它们
.followRedirects(true)
.method(Connection.method.POST)
.execute();
System.out.println(“HTTP状态代码:+responsePostLogin.statusCode());
//从响应解析文档
Document Document=responsePostLogin.parse();
系统输出打印项次(文件);
//去拿饼干
Map mapLoggedInCookies=responsePostLogin.cookies();
/*
*对于所有后续请求,您需要发送
*MapLoggedInCookie包含Cookie
*/
Document doc=Jsoup.connect(“https://www.icloudemserp.com/corecampus/student/student_index.php")
.cookies(mapLoggedInCookies)
.userAgent(userAgent)
.get();
3.系统。输出。系统。输出。印刷LN(系统。系统。系统。输出。系统。输出。印印印(“(“(“联合35)))系统。系统。系统。系统。系统。系统。系统。系统。系统。输出。系统。系统。输出。系统。输出。系统。印印民民民民民方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方####################;
系统输出打印项次(doc);
}
谢谢您的帮助:)您对您所说的后续需求有问题。它重定向到登录页面。你应该把注意力集中在用你的凭证发布后得到的回复上。你正在打印回复,内容是什么?事实上,我对网页抓取是新手,所以我在寻找网页的html内容,这也会帮助我知道我是否正确登录。