【问题标题】:Retrieve the final redirect url with HttpUnit使用 HttpUnit 检索最终的重定向 url
【发布时间】:2013-03-28 10:50:37
【问题描述】:

我正在使用 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());
    }
}

【问题讨论】:

    标签: java http web-crawler http-unit


    【解决方案1】:

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

    【讨论】:

    • 我的问题不在于表单提交,而在于重定向网址
    • 提交表单后,您会收到重定向您的 HTTP 请求?我无法访问您在问题中提到的网站,因为它会将我重定向到 Dutch version。
    • 提交表单后,它会将我重定向到搜索页面(请稍候....)如果我们手动提交表单,它会做一些事情,但最终会将我们重定向到结果页面
    • 喜欢比价网站
    • 我不确定 HTTP 单元是否能够处理这个问题,它似乎进行了一些轮询以检查结果是否可用......也许可以尝试 Selenium(它在后台使用真正的浏览器)?
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2014-01-15
    • 2014-12-07
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多