HtmlUnit Session Management

I am trying to enter a Facebook page using HtmlUnit and view its HTML content. I am trying to fill in the login credentials through HtmlUnit, but I do not see that the session is transferred when the submit button is clicked.

Could not find a lot of content for the htmlunit session management classes. I also added the code that I am using now to try to solve this problem. Any help appreciated!

WebClient webClient = new WebClient(); HtmlPage page1 = webClient.getPage("https://www.facebook.com"); List<HtmlForm> listF = page1.getForms(); HtmlForm form = null; for(int i=0; i<listF.size(); i++) { if(listF.get(i).getId().equals("login_form")) { form = listF.get(i); } } HtmlTextInput uName = form.getInputByName("email"); HtmlPasswordInput passWord = form.getInputByName("pass"); HtmlSubmitInput button = form.getInputByValue("Log In"); uName.setValueAttribute(FACEBOOK_UNAME); passWord.setValueAttribute(FACEBOOK_PASS); HtmlPage page2 = button.click(); 
+5
source share
2 answers

Found the answer. Just enable cookies before you start receiving web pages. It is working. The following code snippet is added.

 WebClient webClient = new WebClient(); CookieManager cookieMan = new CookieManager(); cookieMan = webClient.getCookieManager(); cookieMan.setCookiesEnabled(true); 
+9
source

One more tip if you are trying to restore an HTTP session to HtmlUnit instead of using webClient.getCookieManager().addCookie(cookie); use this instead:

 webClient.addCookie("cookieName=cookieValue", URL, null); 
0
source

Source: https://habr.com/ru/post/1441191/


All Articles