package htmlunit;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import com.gargoylesoftware.htmlunit.UnexpectedPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class T {
public static String webSite="https://asp.gigacc.com";
public static String loginUrl = webSite+"/user/gps-cep";
public static String taskUrl = webSite+"/user/user/sharing/list.do?ID=q8gq6icsffrvb9s2iru5p2aqo0&t=0";
public static String logoutUrl=webSite+"/user/user/Logout.do";
public static String userName = "username";
public static String password = "password ";
public static void main(String[] args) throws Exception {
loginLinkedin();
}
@SuppressWarnings("resource")
public static void loginLinkedin() throws Exception {
HtmlPage page=null;
try {
WebClient webClient = new WebClient();// 创建WebClient
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
// 获取页面
page = webClient.getPage(loginUrl);// 打开
// 获得name为"session_key"的html元素
HtmlElement usernameEle =page.getHtmlElementById("login_id");
HtmlElement passwordEle =page.getHtmlElementById("password");
usernameEle.focus();// 设置输入焦点
usernameEle.type(userName);// 填写值
passwordEle.focus();// 设置输入焦点
passwordEle.type(password);// 填写值
// 获得name为"submit"的元素
HtmlElement submitEle = page.getElementByName("I3thdXRoZW50aWNhdGlvbkFjdGlvbi5hdXRoZW50aWNhdGlvbn0=");
// 点击“登陆”
page = submitEle.click();
String result = page.asXml();// 获得click()后的html页面(包括标签)
// System.out.println(result);
//The account has already been logged in.
if(result.contains("The account has already been logged in.")) {
System.out.println("重复登陆");
//找到确认登录按钮
DomNodeList<DomElement> list=page.getElementsByTagName("input");
for (DomElement e : list) {
if(e.getAttribute("type").equals("submit")) {
System.out.println("确认登录");
page = e.click();
break;
}
}
}
System.out.println("----------------------华丽的分割线---------------------------");
//System.out.println(page.asText());
//跳转到固定
DomElement de=page.createElement("a");
de.setAttribute("href", taskUrl);
page=de.click();
DomNodeList<DomElement> list=page.getElementsByTagName("a");
for (DomElement domElement : list) {
String md5="cep_pak_bin.md5";
String time=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
if(domElement.getAttribute("title").equals(md5)) {
String downloadUrl=webSite+domElement.getAttribute("href");
System.out.println("md5文件 "+downloadUrl);
saveFile(downloadUrl,page, time+md5);
}
String bin="cep_pak.bin";
if(domElement.getAttribute("title").equals(bin)) {
String downloadUrl=webSite+domElement.getAttribute("href");
System.out.println("bin文件 "+downloadUrl);
saveFile(downloadUrl,page, time+bin);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//user/user/Logout.do 退出登录
if(page!=null) {
DomElement de=page.createElement("a");
de.setAttribute("href", logoutUrl);
page=de.click();
System.out.println("退出成功");
// System.out.println(page.asXml());
}
}
}
public static void saveFile(String href,HtmlPage page, String file) throws Exception {
file="E:/zxg/"+file;
DomElement de=page.createElement("a");
de.setAttribute("href", href);
UnexpectedPage tmppage=de.click();
InputStream is = tmppage.getWebResponse().getContentAsStream();
FileOutputStream output = new FileOutputStream(file);
IOUtils.copy(is, output);
output.close();
}
}
htmlunit 模拟登录 并下载文件
最新推荐文章于 2024-01-22 10:20:38 发布