How can I tell HtmlUnit's WebClient to download images and css?

后端 未结 4 426
独厮守ぢ
独厮守ぢ 2020-12-11 15:33

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

4条回答
  •  不知归路
    2020-12-11 16:32

    Here's what I came up with:

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

    My tests show, that it does support proxys and that it not only carries cookies from WebClient, but also if server sends new cookies during the response, the WebClient will eat those cookies

提交回复
热议问题