如何将Cookie添加到HtmlUnit请求标头? [英] How can I add cookies to HtmlUnit request header?

查看:1038
本文介绍了如何将Cookie添加到HtmlUnit请求标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问某个网站,但我无法将收集的Cookie添加到传出的POST请求标头中。我已经能够验证它们是否存在于CookieManager中。

I'm trying to access a site and I'm having trouble adding the "Cookie" collected to outgoing POST request header. I've been able to verify that they are present in the CookieManager.

对HtmlUnit的任何替代方法也将受到赞赏。

Any alternative means to HtmlUnit would also be appreciated.

public static void main( String[] args )
    {
        // Turn off logging to prevent polluting the output.
        Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);

        try {
            final WebClient webClient = new WebClient(BrowserVersion.CHROME);



            webClient.getOptions().setCssEnabled(false);


            CookieManager cookieManager = webClient.getCookieManager();

            out.println(cookieManager.getCookies().toString());

            out.println("start");

            final HtmlPage loginPage = webClient.getPage("my_url");


            Map<?, ?> additionalRequest1 = loginPage.getWebResponse().getWebRequest().getAdditionalHeaders();

            Iterator<?> ite0 = additionalRequest1.entrySet().iterator();
             while(ite0.hasNext()){
                 out.println(ite0.next());
             }
             out.println("\n");

            out.println("after loginPage");



            out.println(cookieManager.getCookies().toString());

            Set<Cookie> cookies = new HashSet<Cookie>();
            cookies.addAll(webClient.getCookieManager().getCookies());
            StringBuilder cookieHeader = new StringBuilder();

            Iterator<Cookie> ite = cookies.iterator();
            while (ite.hasNext()){
                Cookie cookie = ite.next();
                cookie.getDomain().substring(1);
                String name = cookie.getName();
                String value = cookie.getValue();

                System.out.println("Cookie:" + name + "=" +value);
                webClient.addRequestHeader(name, value);
            }


            final HtmlTextInput login = (HtmlTextInput) loginPage.getElementById("login");
            login.setValueAttribute(USER_EMAIL);

            final HtmlPasswordInput password = (HtmlPasswordInput) loginPage.getElementById("password");
            password.setValueAttribute(USER_PASS);


            final HtmlSubmitInput button_submit = loginPage.getElementByName("login_submit");

            final HtmlPage accessGrantingPage = button_submit.click();
            final HtmlForm  requestForm = (HtmlForm)accessGrantingPage.getElementById("consent_form");

            Map<?, ?> additionalRequest = accessGrantingPage.getWebResponse().getWebRequest().getAdditionalHeaders();

            Iterator<?> ite2 = additionalRequest.entrySet().iterator();
             while(ite2.hasNext()){
                 out.println(ite2.next());
             }
             out.println("\n");

            out.println("after accessGrantingPage");

            out.println(cookieManager.getCookies().toString());
            final HtmlButton consent_accept_button = accessGrantingPage.getElementByName("consent_accept");

            try {
                final HtmlPage authorizationPage = consent_accept_button.click();
                out.println("after authorizationPage");
                out.println(authorizationPage.getUrl().toString());
                out.println(authorizationPage.getWebResponse().getStatusMessage());

                out.println(authorizationPage.getWebResponse().getResponseHeaders());

            } catch (RuntimeException re){
                re.printStackTrace();
            }
            webClient.closeAllWindows();
        } catch (IOException ioe){
            ioe.printStackTrace();
        } finally {

        }  

    }


推荐答案

我发现我可以使用WebClient中的setadditionalHeader()添加标题。

I found out that I can add the header by using setadditionalHeader() from WebClient.

        for (int index = 0; index < boxCookies.size(); index++){
            String cookie = boxCookies.toArray()[index].toString();
            String cookieNameValue =cookie.substring(0, cookie.indexOf(";"));
            String name = cookieNameValue.substring(0, cookieNameValue.indexOf("="));
            String value = cookieNameValue.substring(cookieNameValue.indexOf("=") + 1);

            if (index == 0){
                cookieHeader.append(name + "=" +value);
            } else {
                cookieHeader.append("; "+ name + "=" +value);
            }

        }
        WebRequest secondLoginPage = new WebRequest(AUTHORIZE_URL);
        secondLoginPage.setAdditionalHeader("Cookie", cookieHeader.toString());
        HtmlPage loginPage2 = webClient.getPage(secondLoginPage);

这篇关于如何将Cookie添加到HtmlUnit请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆