美文网首页
htmlunit应用一例(获取cookie,向网页输出验证码)

htmlunit应用一例(获取cookie,向网页输出验证码)

作者: yahzon | 来源:发表于2020-01-07 12:00 被阅读0次

为啥这么绕。因为自己的页面提交到第三方网站后台,获取结果后返回用户。验证码用户自己填,咱不自动识别。
上代码;action使用spring MVC为例

/**
 *  代替用户请求第三方网站信息 ;
 * @author 大魔王
 *
 */
@Controller
@RequestMapping("/wlcrawler")
public class WlCrawlerCtrl {
  /**
     *  请求第三方页面 ,根据Id获取图片验证码并 返回验证码图片文件流<br/>
     *  因为页面有js代码执行,因此使用HTTPUnit比较方便获取cookie     */ 
    @GetMapping(value = "/wlvalidate_captchar")
    public void wlvalidate(HttpServletRequest request,HttpServletResponse response) throws Exception {      
        
        String str_uri = "https://xxxxxx/platform/validate.html?src=2" ;
        WebClient webClient = new WebClient();
        //启动cookie
        webClient.getCookieManager().setCookiesEnabled(true);
        // 启动 js 解释器
        webClient.getOptions().setJavaScriptEnabled(true);
        // 禁用 css 支持 提高速度
        webClient.getOptions().setCssEnabled(false);
        // ajax
        webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 
        
        //执行请求 需要等待几秒,目的是加载js并执行js代码
        HtmlPage page = webClient.getPage(str_uri);
        webClient.getOptions().setTimeout(10000);
        webClient.waitForBackgroundJavaScript(4000);
        
        java.util.Set<com.gargoylesoftware.htmlunit.util.Cookie> cookies = 
                webClient.getCookieManager().getCookies();
        //观察一下cookie
                cookies.stream().forEach(item -> {
            System.out.println(item.getName() + " ---------- " + item.getValue());
        });

                //保存cookie到咱们自己的session里面
                request.getSession().setAttribute("cookie_xxx",cookies);
                        
        //验证码图片captchar
        com.gargoylesoftware.htmlunit.html.DomElement elem = page.getElementById("checkCode1");
        HtmlImage captchar = (HtmlImage) elem;      
        // 输入部分,也挺简单,图片文件流输出到response.getOutputStream就行
        java.io.OutputStream output = response.getOutputStream();
        javax.imageio.ImageReader imageReader = captchar.getImageReader();
        javax.imageio.ImageIO.write(imageReader.readAsRenderedImage(0, null), "png", output);         
        
        webClient.close();
        
        
    }
}

验证码图片使用:

<form id="form1" name="form1" action="myAction">
        
            <label class="label">身份证号:</label>
            <div class="input_line">
            <input id="carno" type="text" placeholder="请输入身份证号码"/>
            </div>
            
            <label class="label">验证码:</label>
            <div class="input_line">
                <input id="captchar" type="text" placeholder="请输入验证码" />
                <img src="/public-platform/wlcrawler/wlvalidate_captchar" name="captImg" onclick="chCaptchar()"
                    alt="点击重试" title="点击刷新"  />
            </div>
            
            <div id="submit-btn" style="margin-left:60px" class="search-submit" onclick="submit()">
                &nbsp;Q&nbsp;&nbsp;查询&nbsp;
            </div>
            
</form>

第三方cookie的使用
在用户提交请求的myAction中:

request.getSession().getAttribute("cookie_xxx");

算原创吧,参考了一些网页,但是都不是我想要的。

相关文章

网友评论

      本文标题:htmlunit应用一例(获取cookie,向网页输出验证码)

      本文链接:https://www.haomeiwen.com/subject/qauxactx.html