-
-
Notifications
You must be signed in to change notification settings - Fork 185
Description
I have poster a question in stuckoverflow, but no response yet i have tried many configuration but also not work.
question: https://stackoverflow.com/questions/59693423/htmlunit-login-to-page-with-redirection
there is my senario:
- Request page of categories: https://sycm.aliexpress.com/api/public/categories?lang=en_US (for information the response is a json)
- Page requested is secure, so a redirection to login page is made
here is the code:
`package com.test.report;
import com.gargoylesoftware.htmlunit.;
import com.gargoylesoftware.htmlunit.html.;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
public class TestLogin {
static String categoriesUrl = "https://sycm.aliexpress.com/api/public/categories?lang=en_US";
static String loginUrl="https://sycm.aliexpress.com/api/public/categories?lang=en_US";
static String loginForm = "login-form";
static String loginId = "fm-login-id";
static String passwordId = "fm-login-password";
static String btnSubmitId = "fm-login-submit";
static String login = "mylogin";
static String password = "mypass";
public static void main(String[] args) throws IOException {
WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page1 = null;
try {
page1 = webClient.getPage(loginUrl);
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
webClient.waitForBackgroundJavaScript(10000);
String htmlBody = page1.getWebResponse().getContentAsString();
System.out.println(htmlBody);
} catch (IOException e) {
e.printStackTrace();
}
HtmlForm form = (HtmlForm) page1.getElementById(loginForm);
HtmlTextInput textField = (HtmlTextInput) page1.getElementById(loginId);
textField.click();
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
webClient.waitForBackgroundJavaScript(10000);
textField.type(login);
HtmlPasswordInput textField2 = (HtmlPasswordInput) page1.getElementById(passwordId);
textField2.click();
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
webClient.waitForBackgroundJavaScript(10000);
textField2.type(password);
//HtmlSubmitInput button = (HtmlSubmitInput) page1.getElementById(btnSubmitId);
try {
HtmlButton button = (HtmlButton) form.getButtonByName("");
HtmlPage page2 = button.click();
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
webClient.waitForBackgroundJavaScript(10000);
String htmlBody = page2.getWebResponse().getContentAsString();
System.out.println(htmlBody);
webClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}`