本文主要是介绍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); // 得到搜索FormHtmlButtonInput 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 blockSystem.out.println("Exception:" + e.toString());
//} // TODO Auto-generated catch block// // TODO Auto-generated catch blockfinally {
// // 关闭客户端,释放内存webClient.close();}//String result = UrlUtil.sendGet(url, param);// System.out.println(result);// assertNotNull(result);}
这篇关于NetBeans8.2配置htmlunit的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!