我正在尝试访问某个网站,但我无法将收集的“Cookie”添加到传出的POST请求标头中.我已经能够验证它们是否存在于CookieManager中.
任何替代HtmlUnit的方法也将受到赞赏.
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 {
}
}
解决方法:
我发现我可以使用WebClient中的setadditionalHeader()添加标题.
for (int index = 0; index < boxCookies.size(); index++){
String cookie = boxCookies.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);
标签:java,htmlunit 来源: https://codeday.me/bug/20190727/1555663.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。