2010-02-11 8 views

risposta

6

Quello che sto facendo in questo momento è:

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"); 
} 
0

HtmlUnit non scarica CSS o immagini. Essi sono inutili per un browser senza testa ...

ultima volta che ho sentito parlare di esso è qui, ma il biglietto è contrassegnato come privato: http://osdir.com/ml/java.htmlunit.devel/2007-01/msg00021.html

+1

Che cosa succede se l'utente vuole verificare il css o le immagini con un browser headless? Questo sembra essere quello che è implicito nella domanda. Immagino che il css e le immagini non sarebbero inutili, giusto? In effetti, questo è quello che mi ha portato a questa domanda, sarebbe bello se potessi usare un browser headless per controllare un'immagine per dimensione o hash o un css per i valori di un colore di sfondo. Cercando di aiutare qui ... la tua risposta viene fuori un po 'polemica piuttosto che costruttiva. – fooMonster

1

fonte: 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()); 
1

Ecco cosa mi si avvicinò con:

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(); 
} 

miei test dimostrano, che lo fa proxy di supporto e che porta non solo i cookie dal WebClient, ma anche se server invia nuovi cookie durante la risposta, il WebClient mangerà quei biscotti

Problemi correlati