zhangjie_java 2015-08-21 09:55 采纳率: 50%
浏览 2663
已结题

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("\n\n\n");
    System.out.println(page2.asText());
  • 写回答

2条回答 默认 最新

  • devmiao 2015-08-21 15:16
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效