Java – how to add cookies to the htmlunit request header?
•
Java
I'm trying to visit a website, but I can't add the collected "cookies" to the outgoing post request header I have been able to verify that they exist in cookiemanager
Any alternative to htmlunit will also be appreciated
public static void main( String[] args )
{
// Turn off logging to prevent polluting the output.
Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
try {
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false);
CookieManager cookieManager = webClient.getCookieManager();
out.println(cookieManager.getCookies().toString());
out.println("start");
final HtmlPage loginPage = webClient.getPage("my_url");
Map<?,?> additionalRequest1 = loginPage.getWebResponse().getWebRequest().getAdditionalHeaders();
Iterator<?> ite0 = additionalRequest1.entrySet().iterator();
while(ite0.hasNext()){
out.println(ite0.next());
}
out.println("\n");
out.println("after loginPage");
out.println(cookieManager.getCookies().toString());
Set<Cookie> cookies = new HashSet<Cookie>();
cookies.addAll(webClient.getCookieManager().getCookies());
StringBuilder cookieHeader = new StringBuilder();
Iterator<Cookie> ite = cookies.iterator();
while (ite.hasNext()){
Cookie cookie = ite.next();
cookie.getDomain().substring(1);
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("Cookie:" + name + "=" +value);
webClient.addRequestHeader(name,value);
}
final HtmlTextInput login = (HtmlTextInput) loginPage.getElementById("login");
login.setValueAttribute(USER_EMAIL);
final HtmlPasswordInput password = (HtmlPasswordInput) loginPage.getElementById("password");
password.setValueAttribute(USER_PASS);
final HtmlSubmitInput button_submit = loginPage.getElementByName("login_submit");
final HtmlPage accessGrantingPage = button_submit.click();
final HtmlForm requestForm = (HtmlForm)accessGrantingPage.getElementById("consent_form");
Map<?,?> additionalRequest = accessGrantingPage.getWebResponse().getWebRequest().getAdditionalHeaders();
Iterator<?> ite2 = additionalRequest.entrySet().iterator();
while(ite2.hasNext()){
out.println(ite2.next());
}
out.println("\n");
out.println("after accessGrantingPage");
out.println(cookieManager.getCookies().toString());
final HtmlButton consent_accept_button = accessGrantingPage.getElementByName("consent_accept");
try {
final HtmlPage authorizationPage = consent_accept_button.click();
out.println("after authorizationPage");
out.println(authorizationPage.getUrl().toString());
out.println(authorizationPage.getWebResponse().getStatusMessage());
out.println(authorizationPage.getWebResponse().getResponseHeaders());
} catch (RuntimeException re){
re.printStackTrace();
}
webClient.closeAllWindows();
} catch (IOException ioe){
ioe.printStackTrace();
} finally {
}
}
Solution
I found that I can use setadditionalheader () in webclient to add titles
for (int index = 0; index < @R_187_2419@Cookies.size(); index++){
String cookie = @R_187_2419@Cookies.toArray()[index].toString();
String cookieNameValue =cookie.substring(0,cookie.indexOf(";"));
String name = cookieNameValue.substring(0,cookieNameValue.indexOf("="));
String value = cookieNameValue.substring(cookieNameValue.indexOf("=") + 1);
if (index == 0){
cookieHeader.append(name + "=" +value);
} else {
cookieHeader.append("; "+ name + "=" +value);
}
}
WebRequest secondLoginPage = new WebRequest(AUTHORIZE_URL);
secondLoginPage.setAdditionalHeader("Cookie",cookieHeader.toString());
HtmlPage loginPage2 = webClient.getPage(secondLoginPage);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
