问题描述
- htmlunit模拟登录 请高人解答
-
使用htmlunit模拟登录的时候 ,首先穿创建了一个webclient 用webclient请求登录页面,同事请求解析验证码,验证码解析正确,然后开始登录系统,一直提示验证码错误。WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_11);
webClient.getOptions().setJavaScriptEnabled(true);webClient.getOptions().setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(false);webClient .getOptions().setUseInsecureSSL(true);
WebRequest request1 = new WebRequest(new URL("http://rd2.zhaopin.com/s/loginmgr/login.asp")); HtmlPage htmlPage = webClient.getPage(request1); //第三步 获取界面元素 HtmlInput userName = htmlPage.getHtmlElementById("LoginName"); HtmlInput password = htmlPage.getHtmlElementById("Password"); HtmlInput checkCode = htmlPage.getHtmlElementById("CheckCode"); HtmlInput btn = htmlPage.getHtmlElementById("login"); WebRequest request = new WebRequest(new URL("https://passport.zhaopin.com/checkcode/imgrd")); UnexpectedPage imagePage = webClient.getPage(request); InputStream in = imagePage.getWebResponse().getContentAsStream(); File storeFile = new File("D:/zlzp.png"); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(storeFile)); byte[] buf = new byte[2048]; int length = in.read(buf); while (length != -1) { output.write(buf, 0, length); length = in.read(buf); } in.close(); output.close(); String valCode = new OCR().recognizeText(new File("D:\zlzp.png"), "png"); System.out.println(valCode); userName.setAttribute("value", ""); password.setAttribute("value", ""); checkCode.setAttribute("value", valCode); HtmlPage page2 = btn.click(); System.out.println("nnn"); System.out.println(page2.asText());
解决方案
http://foxswily.iteye.com/blog/644353
解决方案二:
http://www.cnblogs.com/skillCoding/archive/2011/12/26/2169982.html
时间: 2024-10-27 17:06:38