zoukankan      html  css  js  c++  java
  • 利用htmlunit登陆带验证码图片的网站

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

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

    2011年09月15日 ⁄ 编程语言 ⁄ 共 1266字 ⁄ 字号    ⁄ 暂无评论 ⁄ 阅读 7,088 次

    以百度统计为例,说明下如何用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();
    }
  • 相关阅读:
    7-11
    7-9
    7-8
    7-7
    7-6
    7-5
    7-4
    7-3
    第08次:升级《陋习手记》完善主从UI
    第07次:升级《陋习手记》显示多条数据
  • 原文地址:https://www.cnblogs.com/donaldlee2008/p/5304504.html
Copyright © 2011-2022 走看看