0

I have this code but I couldn't find best solution for this. I have no idea on the cookie side. here is the code & Warning Cookie.

I'm trying to login for the website after that I use getPage to download a file then write it into my Directory.

String url = "https://www.frw.co.uk/login";
    final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_10);

    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getCookieManager().setCookiesEnabled(true);
    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

    try {

        HtmlPage login = webClient.getPage(url);//button.click();

        final HtmlForm form = (HtmlForm) login.getElementById("loginForm");

        form.getInputByName("j_username").setValueAttribute("example@hotmail.com");
        form.getInputByName("j_password").setValueAttribute("examplepassword");

        HtmlPage reslogin = form.getInputsByValue("Login").get(0).click();

        //reslogin = webClient.getPage("https://www.frw.co.uk/excel/download");

        HtmlPage downloadPage = reslogin.getAnchorByText("Download Wine List").click();

        downloadPage = webClient.getPage("https://www.frw.co.uk/excel/download");


        WebResponse response2 = downloadPage.getWebResponse();
        InputStream is = response2.getContentAsStream();
        OutputStream outputStream = new FileOutputStream(new File(fileTarget));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
        outputStream.flush();
        outputStream.close();

        System.out.println("Cookie "+webClient.getCookieManager().getCookies());

        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
    }

    webClient.closeAllWindows();

WARNING RESULT:

WARNING: Invalid cookie header: "Set-Cookie: fixed_external_2276360454_end_user_id=; Domain=.optimizely.com; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=-1". Negative max-age attribute: -1

WARNING: Cookie rejected: "[version: 0][name: end_user_id][value: oeu1447313832962r0.4258916180646556][domain: .2276360454.log.optimizely.com][path: /js][expiry: Sun Nov 09 15:37:28 CST 2025]". Illegal domain attribute "2276360454.log.optimizely.com". Domain of origin: "cdn.optimizely.com"
Tiborsio_
  • 63
  • 1
  • 13
  • The cookie manager throws the warnings. See [the cookie handler class here](https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/BasicMaxAgeHandler.java). – Koshinae Nov 12 '15 at 09:41
  • `INTERNET_EXPLORER_10` means you use an old HtmlUnit version, please use latest `2.19`, and repost your question. – Ahmed Ashour Nov 12 '15 at 11:19
  • I have updated the HtmlUnit version still cookie is rejected. I'm really having hard time on this. – Tiborsio_ Nov 16 '15 at 07:59

0 Answers0