WebClient 基本使用,模仿网页请求【L】

来源:互联网 发布:数据库无法写入中文 编辑:程序博客网 时间:2024/05/21 17:32
package com.lxl.htmlutil.test;import java.io.IOException;import org.junit.Test;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.DomElement;import com.gargoylesoftware.htmlunit.html.DomNodeList;import com.gargoylesoftware.htmlunit.html.HtmlAnchor;import com.gargoylesoftware.htmlunit.html.HtmlElement;import com.gargoylesoftware.htmlunit.html.HtmlPage;import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;public class HtmlUtilTest {@Testpublic void test() throws IOException {// 创建浏览器,可以选择IE、FF等等WebClient client = new WebClient(); //htmlunit 对css和javascript的支持不好,所以请关闭之client.getOptions().setJavaScriptEnabled(false);client.getOptions().setCssEnabled(false);// 获取某网站页面HtmlPage loginPage = client.getPage("http://175.43.121.77/mblog/login");HtmlElement passwordElmt = loginPage.getElementByName("password");passwordElmt.click();passwordElmt.type("3323356");// 获取某页面元素,可通过id或name,(具体方式很多 --Foxswily)HtmlElement usernameElmt = loginPage.getElementByName("username");// HtmlElement elmt = page.getElementByName("somename");// 此例以文本框为例,先点击,再输入,完全跟实际浏览器行为一致usernameElmt.click();usernameElmt.type("q260996583");// 获取按钮DomNodeList<DomElement> loginBtn = loginPage.getElementsByTagName("input");// HtmlSubmitInput loginBtn = (HtmlSubmitInput) loginPage// .getElementById("LoginAction_login");// HtmlButton loginBtn = (HtmlButton)// page.getElementById("LoginAction_login");HtmlSubmitInput input = null;for(DomElement dom : loginBtn) {if(dom.getAttribute("type").equals("submit")){input = (HtmlSubmitInput) dom;}}// 点击并获得返回结果HtmlPage loginResultPage = input.click();// 结果拿到了,想干啥您随意String loginResultContent = loginResultPage.getWebResponse().getContentAsString();System.out.println(loginResultContent);// 如果登录成功if (null != loginResultContent&& loginResultContent.indexOf("龙马") > 0) {// 获得HTML链接并点击该链接HtmlAnchor showbookBtn = (HtmlAnchor) loginResultPage.getElementById("showbook");HtmlPage showBookPage = showbookBtn.click();System.out.println("\n\n===========================================================");System.out.println(showBookPage.getWebResponse().getContentAsString());}}}

pom.xml

htmlunit 在httpclient项目上增加新功能,htmlunit中包含了httpclient、httpmime等jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.lxl.test</groupId>  <artifactId>SpringSplit</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>SpringSplit Maven Webapp</name>  <url>http://maven.apache.org</url>    <dependencies>   <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit --><dependency>    <groupId>net.sourceforge.htmlunit</groupId>    <artifactId>htmlunit</artifactId>    <version>2.15</version></dependency>  </dependencies>    <build>    <finalName>htmlutil</finalName>  </build></project>