今天研究了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);
}