tags:

views:

430

answers:

4

Hello. How can I make WebClient download external css stylesheets and image bodies just like a usual web browser does?

A: 

Hi, If you have come across the solution already please do let us know. Veena

Veena
Veena, please post such such items as comments, not answers.
skaffman
+1  A: 

HtmlUnit does not download CSS or images. They are useless to a headless browser...

Last I heard of it is here, but the ticket is marked private: http://osdir.com/ml/java.htmlunit.devel/2007-01/msg00021.html

A: 

What I'm doing right now is:

public static final HashMap<String, String> acceptTypes = new HashMap<String, String>(){{
        put("html", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        put("img", "image/png,image/*;q=0.8,*/*;q=0.5");
        put("script", "*/*");
        put("style", "text/css,*/*;q=0.1");
    }};

protected void downloadCssAndImages(HtmlPage page) {
        String xPathExpression = "//*[name() = 'img' or name() = 'link' and @type = 'text/css']";
        List<?> resultList = page.getByXPath(xPathExpression);

        Iterator<?> i = resultList.iterator();
        while (i.hasNext()) {
            try {
                HtmlElement el = (HtmlElement) i.next();

                String path = el.getAttribute("src").equals("")?el.getAttribute("href"):el.getAttribute("src");
                if (path == null || path.equals("")) continue;

                URL url = page.getFullyQualifiedUrl(path);

                WebRequestSettings wrs = new WebRequestSettings(url);
                wrs.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toString());

                client.addRequestHeader("Accept", acceptTypes.get(el.getTagName().toLowerCase()));
                client.getPage(wrs);
            } catch (Exception e) {}
        }



client.removeRequestHeader("Accept");
}
roddik
A: 

source : http://efreedom.com/Question/1-3068543/Get-Base64-Encoded-Contents-ImageReader

HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);

ImageReader imageReader = img.getImageReader();

BufferedImage bufferedImage = imageReader.read(0);

String formatName = imageReader.getFormatName();

ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();

Base64OutputStream base64Output = new base64OutputStream(byteaOutput);

ImageIO.write(bufferedImage, formatName, base64output);

String base64 = new String(byteaOutput.toByteArray());

jer