Java 使用HttpUnit检索最终重定向url
我正在使用HttpUnit模拟此网站: 这是我的代码:它不会向我发送最终的重定向url,只是用于搜索的url,而不是结果Java 使用HttpUnit检索最终重定向url,java,http,web-crawler,http-unit,Java,Http,Web Crawler,Http Unit,我正在使用HttpUnit模拟此网站: 这是我的代码:它不会向我发送最终的重定向url,只是用于搜索的url,而不是结果 public class TestHttpUnit { public static void main(String[] args) throws Exception { // Create and initialize WebClient object WebClient webClient = new WebClient(Bro
public class TestHttpUnit {
public static void main(String[] args) throws Exception {
// Create and initialize WebClient object
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
webClient.setThrowExceptionOnScriptError(false);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});
// visit Yahoo Mail login page and get the Form object
HtmlPage page = (HtmlPage) webClient.getPage("http://www.voyages-sncf.com/");
//Trouver le formulaire par le nom
HtmlForm form = page.getFormByName("TrainTypeForm");
//Trouver le formulaire avec l'action
//HtmlForm form = page.getFirstByXPath("//form[@action='http://www.voyages-sncf.com/dynamic/expressbooking/_SvExpressBooking']");
// Enter login and password of
form.getInputByName("origin_city").setValueAttribute("paris");
form.getInputByName("destination_city").setValueAttribute("marseille");
form.getInputByName("outward_date").setValueAttribute("29/03/2013");
// Click "Sign In" button/link
page = (HtmlPage) form.getInputByValue("Rechercher").click();
System.out.println(page.asText());
}
}
根据表格,表格提交应按以下方式处理:
WebForm form = resp.getForms()[0]; // select the first form in the page
// ... fill in form fields, and finally:
form.submit(); // submit the form
之后,您将进入“等待”屏幕。我还没有试过,但也许你应该做一些类似于以下伪代码的事情:
do {
// Wait a while
wc.waitForBackgroundJavaScript(3000);
// Somehow refresh the page, since that's what your browser might do, too
} while (someTestToVerifyThatYoureStillOnTheWaitingPage(wc));
我的问题不在表单提交中,而是在重定向URL中。提交表单后,您会收到一个重定向您的HTTP请求吗?我无法访问您在问题中提到的网站,因为它将我重定向到。提交表单后,它将我重定向到搜索页面,请稍候。。。。如果我们手动提交表单,它会做一些事情,但它会在最后将我们重定向到结果页面,就像price comparatorI的站点一样。我不确定HTTP Unit是否能够处理这个问题,它似乎会进行一些轮询,以检查结果是否可用。。。也许可以试试Selenium,它在引擎盖下使用真正的浏览器?