gpt4 book ai didi

java - 使用 HttpUnit 检索最终重定向 url

转载 作者:行者123 更新时间:2023-12-01 04:46:54 25 4
gpt4 key购买 nike

我正在使用 HttpUnit 来模拟此网站:http://www.voyages-sncf.com/这是我的代码:它不会向我发送最终的重定向 url,只是发送搜索的 url,而不是结果

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());
}
}

最佳答案

根据HTTP Unit cookbook ,表单提交应该这样处理:

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));

关于java - 使用 HttpUnit 检索最终重定向 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15679846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com