如何使WebClient下载外部css样式表和图像主体,就像通常的Web浏览器一样?
答案 0 :(得分:6)
我现在正在做的是:
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");
}
答案 1 :(得分:1)
来源:How to get base64 encoded contents for an 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());
答案 2 :(得分:1)
以下是我提出的建议:
public InputStream httpGetLowLevel(URL url) throws IOException
{
WebRequest wrq=new WebRequest(url);
ProxyConfig config =webClient.getProxyConfig();
//set request webproxy
wrq.setProxyHost(config.getProxyHost());
wrq.setProxyPort(config.getProxyPort());
wrq.setCredentials(webClient.getCredentialsProvider().getCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort())));
for(Cookie c:webClient.getCookieManager().getCookies(url)){
wrq.setAdditionalHeader("Cookie", c.toString());
}
WebResponse wr= webClient.getWebConnection().getResponse(wrq);
return wr.getContentAsStream();
}
我的测试显示,它确实支持代理,并且它不仅携带来自WebClient的cookie,而且如果服务器在响应期间发送新的cookie,WebClient将会吃掉这些cookie
答案 3 :(得分:0)
HtmlUnit不下载CSS或图像。它们对无头浏览器毫无用处......
最后我听说它在这里,但是票证标记为私人:http://osdir.com/ml/java.htmlunit.devel/2007-01/msg00021.html