日期:2014-05-17 浏览次数:20776 次
package com.htmlunit.sky; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.CollectingAlertHandler; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.DomNodeList; import com.gargoylesoftware.htmlunit.html.HtmlDivision; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.html.HtmlTable; import com.gargoylesoftware.htmlunit.html.HtmlTableCell; import com.gargoylesoftware.htmlunit.html.HtmlTableRow; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; /** * 运用htmlUnit爬网页 * * @author liming */ public class SkyHtmlUnit { // 充值密码 private static final String transPassword = "xxxx"; // 用户名 private static final String userName = "XXXX"; // 密码 private static final String userPassword = "xxxx"; /** * 充值 * * @param transNumber * 被充值用户号码 * @param transAmount * 充值金额 */ public static boolean recharge(String transNumber, String transAmount) { boolean bool = false; try { BigDecimal b = new BigDecimal(transAmount); b = b.setScale(0, BigDecimal.ROUND_DOWN); WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_8); List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); HtmlPage page1 = (HtmlPage) client .getPage("http://ZXXXX/vams/"); HtmlForm form = (HtmlForm) page1.getForms().get(0); HtmlTextInput username = (HtmlTextInput) form .getInputByName("login:userName"); HtmlPasswordInput password = (HtmlPasswordInput) form .getInputByName("login:password"); username.setValueAttribute(userName); password.setValueAttribute(userPassword); form.getInputByName("login:j_id_jsp_28762255_4").click(); client.setJavaScriptEnabled(true); String Recharge = "http://ZXXXX/vams/WebPay/WebTrans.faces"; HtmlPage Recharge1 = (HtmlPage) client.getPage(Recharge); HtmlForm RechargeForm = (HtmlForm) Recharge1.getForms().get(0); HtmlTextInput number = (HtmlTextInput) RechargeForm .getInputByName("webPayment:transNumber"); HtmlTextInput transAmountt = (HtmlTextInput) RechargeForm .getInputByName("webPayment:transAmount"); HtmlPasswordInput transPass = (HtmlPasswordInput) RechargeForm .getInputByName("webPayment:transPassword"); // 设置充值密码 transPass.setValueAttribute(transPassword); // 设置充值金额 transAmountt.setValueAttribute(b.toString()); // 设置充值号码 number.setValueAttribute(transNumber); RechargeForm.getInputByName("webPayment:j_id_jsp_427518967_10") .click(); String returnvalue = collectedAlerts.get(0); if (returnvalue.equals("充值成功!")) { bool = true; } } catch (Exception e) { e.printStackTrace(); } System.out.println("**********充值结束*******"); return bool; } /** * 充值结果查询 * * @param payNoValue * 充值号码 * @param beforeTime * 充值时间前 格式YYYYMMdd * @pa