来源:https://stackoverflow.com/questions/34314202/java-htmlunit-webclient-ssl-page
public static void main(String[] args)
throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
WebClient wc = initWebClient(null);
HtmlPage page = wc.getPage(Constants.START_PAGE);
HtmlTextInput userInput = (HtmlTextInput) page
.getElementById(Constants.INPUT_USERNAME_ID);
userInput.setText(Constants.USERNAME_VALUE);
HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
.getElementById(Constants.INPUT_PASSWORD_ID);
passwordInput.setText(Constants.PASSWORD_VALUE);
// get submit button
HtmlSubmitInput submitButton = (HtmlSubmitInput) page
.getElementById(Constants.SUBMIT_BUTTON_ID);
HtmlPage afterLoginPage = submitButton.click();
Set<Cookie> cookies = wc.getCookieManager().getCookies();
// for every thread I create new WebClient
for(threads loop){
WebClient wc2 = initWebClient(cookies);
}
}
private static WebClient initWebClient(Set<Cookie> cookies) {
WebClient wc = new WebClient(BrowserVersion.CHROME);
wc.getCookieManager().setCookiesEnabled(true);
wc.getOptions().setJavaScriptEnabled(false);
wc.getOptions().setThrowExceptionOnScriptError(false);
if (cookies != null) {
Iterator<Cookie> i = cookies.iterator();
while (i.hasNext()) {
wc.getCookieManager().addCookie(i.next());
}
}
return wc;
}