简体   繁体   中英

jsoup login and parse html

the problem is: when I login to the website it directs to a home page every time. but I want to connect with different page to parse its html and download from it, so the document in the last connect directs to login again

try {
            
            Connection.Response response = Jsoup.connect("https://10.232.1.220/123Mobile/Portal/")
                    .userAgent(USER_AGENT)
                    .sslSocketFactory(utilService.socketFactory())
                    .method(Connection.Method.GET)
                    .execute();

            //Login 
            
            FormElement loginForm = (FormElement)response.parse().select("form[class=login-form]").first();
            checkElement("Login Form", loginForm);
            Element loginField = loginForm.select("input[name=UserName]").first();
            checkElement("Login Field", loginField);
            loginField.val(internalConstant.getEbcUsename());
            Element passwordField = loginForm.select("input[name=Password]").first();
            checkElement("Password Field", passwordField);
            passwordField.val(internalConstant.getEbcPassword());
            response = loginForm.submit()
                     .cookies(response.cookies())
                     .userAgent(USER_AGENT)  
                     .method(Method.POST)
                     .sslSocketFactory(utilService.socketFactory())
                     .followRedirects(false)
                     .execute();
            
            logger.info("home html: " + response.parse());
            //target document
            
            Document targetPage = Jsoup.connect("https://10.232.1.220/123Mobile/Portal/Reports/TransactionReport")
                    .userAgent(USER_AGENT)
                    .sslSocketFactory(utilService.socketFactory())
                    .cookies(response.cookies())
                    .get();
            
            logger.info("target document html: " + targetPage.html());

            if (ebcFile.isEmpty()) {
                logger.error("file not found");

                throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ebc File not found in website");
            }

            // this.FileDownloaderService.downloadExcelFile(ebcFile);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("download error " + e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Failed to download file");
        }

The problem was the response of submit cookie doesn't has the sessionid and the request token cookies, so I added them from the login response.

Connection.Response response = Jsoup.connect(internalConstant.getLoginURL())
                    .userAgent(USER_AGENT)
                    .sslSocketFactory(utilService.socketFactory())
                    .method(Connection.Method.GET)
                    .execute();
            
            logger.info("response cookies after parse login page: " + response.cookies());
            String sesionID = response.cookie(your cookie);
            String requestCookie = response.cookie(your cookie);

            //Login 
            
            FormElement loginForm = (FormElement)response.parse().select("form[class=login-form]").first();
            checkElement("Login Form", loginForm);
            Element loginField = loginForm.select("input[name=UserName]").first();
            checkElement("Login Field", loginField);
            loginField.val(internalConstant.getEbcUsename());
            
            Element passwordField = loginForm.select("input[name=Password]").first();
            checkElement("Password Field", passwordField);
            passwordField.val(internalConstant.getEbcPassword());
            response = loginForm.submit()
                     .cookies(response.cookies())
                     .userAgent(USER_AGENT)  
                     .method(Method.POST)
                     .sslSocketFactory(utilService.socketFactory())
                     .execute();
            Map<String , String> coky = response.cookies();
            coky.put("your cookie", requestCookie);
            coky.put("your cookie", sesionID);
            
            logger.info("response cookies after submit: " + coky);
            
            
            //target document
            
             response = Jsoup.connect(internalConstant.getURL())
                    .userAgent(USER_AGENT)
                    .sslSocketFactory(utilService.socketFactory())
                    .cookies(coky)
                    .method(Method.GET)
                    .execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM