利用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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM