利用htmlunit登陆带验证码图片的网站

http://htsoft.org/html/y2011/822_using-htmlunit-landing-site-with-captcha-image.html


以百度统计为例,说明下如何用htmlunit登陆带验证码的网站

//baidu统计登陆代码
try
{
	WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
	client.setJavaScriptEnabled(false);
	HttpWebConnection httpwebconnection = new HttpWebConnection(client);
	httpwebconnection.setUseInsecureSSL(true);
	client.setWebConnection(httpwebconnection);
	HtmlPage page = client.getPage("http://tongji.baidu.com");
	HtmlElement username = page.getElementById("UserName");
	HtmlElement password = page.getElementById("Password");
	HtmlElement valiCode = page.getElementById("Valicode");
	HtmlImage valiCodeImg = (HtmlImage) page.getElementById("cas_code");
	ImageReader imageReader = valiCodeImg.getImageReader();
	BufferedImage bufferedImage = imageReader.read(0);

	JFrame f2 = new JFrame();
	JLabel l = new JLabel();
	l.setIcon(new ImageIcon(bufferedImage));
	f2.getContentPane().add(l);
	f2.setSize(100, 100);
	f2.setTitle("验证码");
	f2.setVisible(true);
	
	String valicodeStr = JOptionPane.showInputDialog("请输入验证码:");
	f2.setVisible(false);
	HtmlElement submit = page.getElementById("Submit");
	HtmlSubmitInput submit2 = (HtmlSubmitInput) submit;
	username.click();
	username.type("gabazi");
	password.click();
	password.type("******");
	valiCode.click();
	valiCode.type(valicodeStr);

	HtmlPage resultPage = submit2.click();
	System.out.println(resultPage.asText());
	System.exit(0);
}
catch(Exception e)
{
	e.printStackTrace();
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值