htmlUnit使用

htmlUnit介绍:

htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源实现。是一个没有界面的浏览器,运行速度迅速。官方网址:

http://htmlunit.sourceforge.net/


htmlUnit示例:

pom依赖:

 <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.26</version>
        </dependency>

测试代码

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Test;

import java.io.IOException;


public class BaiduDemo {
    @Test
    public void test() throws IOException {
        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setUseInsecureSSL(false);


        //获取页面
        String url ="https://www.baidu.com";
        HtmlPage page = webClient.getPage(url);

        System.out.println("页面文本:"+page.getTitleText());


        //获取页面元素
        HtmlInput htmlInput = page.getHtmlElementById("kw");
        System.out.println(htmlInput.asText());
        htmlInput.setValueAttribute("test");

        HtmlInput btn = page.getHtmlElementById("su");
        HtmlPage page2 = btn.click();

        System.out.println("页面2:"+page2.getTitleText());
    }
    
}


取cookie:

BasicCookieStore cookieStore = new BasicCookieStore();
        Set<Cookie> cookies2 =webClient.getCookieManager().getCookies();
        for (Cookie cookie : cookies2) {
            cookieStore.addCookie(cookie.toHttpClient());
        }

通过htmlUnit获取的cookie信息发送https请求:


入参: 

cookieStore //通过htmlUnit获取的登陆信息

 url参数:要访问的页面参数

protected String sendRequest(BasicCookieStore cookieStore,String url) throws Exception {
        if (null == cookieStore) {
            return null;
        }

        //设置cookie
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);

        //忽略SSL证书验证

        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            //信任所有
            public boolean isTrusted(X509Certificate[] chain,
                                     String authType) throws CertificateException {
                return true;
            }
        }).build();  
        CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
        HttpGet httpGet = new HttpGet(url);

        //发送请求并获取response
        CloseableHttpResponse response = httpClient.execute(httpGet, context);
        HttpEntity entity = response.getEntity();

        return EntityUtils.toString(entity);
    }


htmlUnit应用场景

todo

相关资料:

http://blog.csdn.net/lifj07/article/details/8638098


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值