2

我正在 HTML 单元的帮助下测试论坛 PhpBB。我想在 PhpBB 论坛的主题之一中添加消息。我在 textarea 中输入消息并单击提交按钮,但新页面没有我的消息。似乎是什么问题?

final ArrayList<HtmlForm> formList=(ArrayList<HtmlForm>) page.getByXPath("//form[@id='postform']");  
final HtmlTextArea myMessage=formList.get(0).getTextAreaByName("message");
myMessage.type("text");
final HtmlSubmitInput submit_post = formList.get(0).getInputByName("post");

page=submit_post.click();

if (page.asText().contains("text")) {
  System.out.println("right");
}

谢谢你的帮助!

4

1 回答 1

0

解决此问题的一种方法是构建自定义 http post 请求。

使用 Fiddler 捕获流量,然后使用 HTMLUnit 进行复制。它应该如下所示

public Page postMessage() throws Exception
{
    URL url = new URL("YOURURL");
    WebRequest requestSettings = new WebRequest(url, HttpMethod.POST);

    requestSettings.setAdditionalHeader("Accept", "*/*");
    requestSettings.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded");
    requestSettings.setAdditionalHeader("Referer", "REFERER");
    requestSettings.setAdditionalHeader("Accept-Language", "en-US,en;q=0.8");
    requestSettings.setAdditionalHeader("Accept-Encoding", "gzip,deflate,sdch");

    requestSettings
            .setRequestBody("YOURPOSTREQUEST");

    Page page = webClient.getPage(requestSettings);

    return page;
}
于 2014-01-02T21:42:46.693 回答