Navigating through a website that requires Login using Jsoup

Viewed 33

I am doing a project for my university and it requires login to the university portal and extracting some data using web scraping technologies, I am currently using java (Jsoup).

I tried to log in by posting the credentials on the login action link and it returns:

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML//EN"><!-- Copyright (C) 2000 Tivoli Systems, Inc. --><!-- Copyright (C) 1999 IBM Corporation --><!-- Copyright (C) 1998 Dascom, Inc. --><!-- All Rights Reserved. -->
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Success</title>
 </head>
 <body onload="history.back()">
 </body>
</html>

And This is The code:

final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,"
\+ "like Gecko) Chrome/51.0.2704.103 Safari/537.36";
final String LOGIN_FORM_URL = "https://banweb.lau.edu.lb/prod/twbkwbis.P_WWWLogin";
final String LOGIN_ACTION_URL = "https://banweb.lau.edu.lb/pkmslogin.form";
final String USERNAME = "";
final String PASSWORD = "";
// # Go to login page and grab cookies sent by server
Connection.Response loginForm;
try {
loginForm = Jsoup.connect(LOGIN_FORM_URL).method(Connection.Method.GET).userAgent(USER_AGENT).execute();

            Document loginDoc = loginForm.parse(); 
            HashMap<String, String> cookies = new HashMap<>(loginForm.cookies());
    
            HashMap<String, String> formData = new HashMap<>();
            // formData.put("commit", "Sign in");
            
            formData.put("username", USERNAME);
            formData.put("password", PASSWORD);
    
            // # Now send the form for login
            
            
            Connection.Response homePage = Jsoup.connect(LOGIN_ACTION_URL).cookies(cookies).data(formData).method(Connection.Method.POST)
                    .userAgent(USER_AGENT).followRedirects(true).execute();
            Document menu = Jsoup.connect(
                    "https://banweb.lau.edu.lb/prod/twbkwbis.P_GenMenu?name=bmenu.P_MainMnu")
                    .response(homePage)
                    .cookies(homePage.cookies()).get();
            System.out.println(homePage.parse().html());
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

\\

However When I try to navigate through the website (in the code the variable menu) and print the page it returns the login page. How can I keep my login success state in order to navigate through the website

0 Answers
Related