简体   繁体   English

使用HttpUnit检索最终的重定向URL

[英]Retrieve the final redirect url with HttpUnit

I am using HttpUnit to simulate this web site : http://www.voyages-sncf.com/ This is my code : It don't sent to me the final redirect url just the url for searching not the result 我正在使用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());
    }
}

According to the HTTP Unit cookbook , form submission should be handled like this: 根据HTTP Unit食谱 ,表单提交应按以下方式处理:

WebForm form = resp.getForms()[0];      // select the first form in the page
// ... fill in form fields, and finally:
form.submit();                          // submit the form

After that, you'll get on the 'waiting' screen. 之后,您将进入“等待”屏幕。 I haven't tried it, but maybe you should do something like this (pseudo code): 我还没有尝试过,但也许您应该执行以下操作(伪代码):

do {
    // Wait a while
    wc.waitForBackgroundJavaScript(3000);
    // Somehow refresh the page, since that's what your browser might do, too
} while (someTestToVerifyThatYoureStillOnTheWaitingPage(wc));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM