如何让 WebClient 像普通的网络浏览器一样下载外部 CSS 样式表和图像主体?
问问题
10083 次
4 回答
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");
}
于 2010-04-29T14:41:03.830 回答
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
于 2011-08-19T10:32:24.470 回答
1
来源:如何获取 ImageReader 的 base64 编码内容?
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());
于 2010-10-17T14:03:12.937 回答
0
HtmlUnit 不下载 CSS 或图像。它们对无头浏览器毫无用处......
上次听说是在这里,但是票被标记为私有: http: //osdir.com/ml/java.htmlunit.devel/2007-01/msg00021.html
于 2010-04-28T22:48:04.223 回答