zhangjie_java
zhangjie_java
2015-08-21 09:42

htmlunit模拟登录 验证码错误

2
  • html5
  • 验证码

使用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);
WebWindow webWindow = webClient.getCurrentWindow();

    WebRequest request1 = new WebRequest(new URL("http://rd2.zhaopin.com/s/loginmgr/login.asp"));
    HtmlPage htmlPage = webClient.getPage(webWindow,request1);
    String name =  htmlPage.getWebClient().getWebWindows().get(0).getName();
    System.out.println(name);
    //第三步 获取界面元素
    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(webWindow,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("\n\n\n");
    System.out.println(page2.asText());
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐