NetBeans8.2配置htmlunit

今天研究了htmlunit。试验了一天,发现htmlunit对于maven的版本匹配特别重要,否则会出现各类错误。包括HttpClientBuilder.sslcontext

或者

org/apache/commons/io/Charsets

 

现在显示标准的Maven配置

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
<dependency>
           <groupId>net.sourceforge.htmlunit</groupId>
           <artifactId>htmlunit</artifactId>
           <version>2.19</version>
        </dependency>
        <dependency>
           <groupId>xerces</groupId>
           <artifactId>xercesImpl</artifactId>
           <version>2.11.0</version>
        </dependency>             
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

 

测试代码如下:

 public void testWebClient() {
        System.out.println("Test WebClient");

        //refresh_token
        //authorization_code
        /**
         * {"access_token":"78ac1f9a-a05b-4f88-85ce-da37ffd7cf2f","refresh_token":"44d08241-ff1b-4365-a959-3151a7838f7b","uid":4079215,"token_type":"bearer","expires_in":604339}
         */
        // String url = "https://www.oschina.net/action/openapi/token";
        // String param = "client_id=IjcSWSsHwEfWuVBCGHw2&client_secret=NfpXfAECeRrDUtXCIj6W9QxneWuSoiNw&grant_type=refresh_token&redirect_uri=http%3A%2F%2F172.26.159.180%2Fcentreon%2F&refresh_token=a06dfbca-1b1c-443a-82cc-a851851030e0";
        // String url = "https://www.oschina.net/action/oauth2/authorize";
        //String param ="client_id=IjcSWSsHwEfWuVBCGHw2&response_type=code&redirect_uri=http%3A%2F%2F172.26.159.180%2Fcentreon%2F";
        WebClient webClient = new WebClient(BrowserVersion.CHROME); // 实例化Web客户端 
        // 这里是配置一下不加载css和javaScript,因为httpunit对javascript兼容性不太好

        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
         webClient.getOptions().setTimeout(50000);

        // String[] sslClientProtocols = {"TLSv1", "TLSv1.1", "TLSv1.2"};
        //webClient.getOptions().setSSLClientProtocols(sslClientProtocols);
        //webClient.getOptions().setUseInsecureSSL(true);
        try {
            HtmlPage page = webClient.getPage("https://www.oschina.net/action/oauth2/authorize?client_id=IjcSWSsHwEfWuVBCGHw2&response_type=code&redirect_uri=http%3A%2F%2F172.26.159.180%2Fcentreon%2Ftest2.php"); // 解析获取页面
            HtmlForm form = page.getElementById("frm_approve", true); // 得到搜索Form

            HtmlButtonInput button = form.getInputByName("authorize");

            HtmlPage page2 = button.click(); // 模拟点击
            //调用JS触发登录按钮
            webClient.waitForBackgroundJavaScript(10000);
            System.out.println("xzy code :" + page2.asXml());

            //System.out.println("code Error");
        } catch (FailingHttpStatusCodeException | IOException e) {
//            // TODO Auto-generated catch block
            System.out.println("Exception:" + e.toString());
//
        } // TODO Auto-generated catch block
        //        // TODO Auto-generated catch block
        finally {
//            // 关闭客户端,释放内存
            webClient.close();

        }

        //String result = UrlUtil.sendGet(url, param);
        // System.out.println(result);
        // assertNotNull(result);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值