我正在用 htmlunit 测试论坛 phpbb3。我在论坛 phpbb 主题中自动添加消息时遇到问题。我需要发布隐藏输入:form_token 和 creation_time。我怎样才能得到它?
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
HtmlPage page = webClient.getPage(URL);
// get input "username"
final HtmlElement user_name = page.getElementsByName("username").get(0);
//enter of value
user_name.type(par[0]);
//get input "password"
final HtmlElement password = page.getElementsByName("password").get(0);
//enter of value
password.type(par[1]);
// get button 'login' and click
final HtmlSubmitInput submit_button = page.getElementByName("login");
page = submit_button.click();
//Go to the page of adding message
page=page.getAnchorByText("Your first forum").click();
page=page.getAnchorByText("Welcome to phpBB3").click();
page=page.getAnchorByHref("./posting.php?mode=reply&f=2&t=1").click();
//get textarea
final HtmlTextArea myMessage=page.getElementByName("message");
/* HtmlInput form_token=page.getElementByName("form_token");
form_token.setAttribute("value", "0");
HtmlInput creation_time=page.getElementByName("creation_time");
creation_time.setAttribute("value", "0");
*/
//enter the valye
myMessage.type("myText");
//get the button 'Submit' and click
final HtmlElement submit_post = page.getElementByName("post");
page=submit_post.click();
//Check that my text is on the page
if (page.asText().contains("myText") {
System.out.println("Yes");
}
else System.out.println("No");
但是在此操作之后,页面上没有新消息。据我所知,服务器检查隐藏字段 form_token 和 creation_time。如何获得正确的字段值?或者你知道解决问题的其他方法吗?