zoukankan      html  css  js  c++  java
  • 谷歌recaptcha验证码java解决方案

     

    谷歌验证码,好几个版本,此处只针对于V2版本。

    1、去网站 https://2captcha.com 注册,获取API  Key 。首次需要充值10美元。

    2、https://2captcha.com/2captcha-api#solving_recaptchav2_new ,查看解决方案。 

    3、这里使用 cdp4j 替代 selinum。最新版本4.0

    4、代码如下。


    import com.util.StringUtil;
    import io.webfolder.cdp.Launcher;
    import io.webfolder.cdp.session.Session;
    import io.webfolder.cdp.session.SessionFactory;
    import io.webfolder.cdp.session.SessionSettings;
    import org.apache.commons.lang3.StringUtils;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;

    public class RecaptchaV2 {

    public static String apiKey = "XXXXXX";
    public static String urlWithRecaptcha = "http://XXXXXX:8080/index3.html";

    public static void main(String[] args) {

    Launcher launcher = new Launcher();
    try (SessionFactory factory = launcher.launch();
    Session session = factory.create(new SessionSettings())) {
    session.navigate(urlWithRecaptcha);
    session.waitDocumentReady(60000);
    String content = session.getContent();

    System.out.println(content);
    String sitekey = getSitekey(content);
    System.out.println("sitekey = " + sitekey);

    // 2
    String url = "https://2captcha.com/in.php?key="+ apiKey +"&method=userrecaptcha&googlekey="+ sitekey +"&pageurl=" + urlWithRecaptcha;

    Document document = Jsoup.connect(url).get();
    System.out.println(document);

    String captchaId = StringUtils.substringAfter(document.text(), "OK|");
    System.out.println("captchaId = " + captchaId);


    // 3
    String documentText;
    do {
    Thread.sleep(5000);
    document = Jsoup.connect("http://2captcha.com/res.php?key="+ apiKey +"&action=get&id=" + captchaId).get();
    documentText = document.text();
    } while (!documentText.contains("OK"));
    String captchaResponse = StringUtils.substringAfter(documentText, "OK|");
    System.out.println("captchaResponse = " + captchaResponse);
    // 4

    session.setAttribute("#g-recaptcha-response", "style", "");

    session.setValue("#g-recaptcha-response", captchaResponse);

                 Thread.sleep(5000);


           // 我自己注册了 google captcha, 前端页面我可以自己控制,这里abc,是页面 submit 元素的ID。
    session.click("#abc");

    Thread.sleep(5000);
            } catch (Exception e) {
    e.printStackTrace();
    } finally {
    launcher.kill();
    }

    }

    /**
    * 获取 data-sitekey 值
    * @param content
    * @return
    */
    public static String getSitekey(String content) {
    if(content == null) {
    return null;
    }

    Element element = Jsoup.parse(content).selectFirst(".g-recaptcha");
    if(element == null) {
    return null;
    }

    return element.attr("data-sitekey");
    }
    }

    运行结果:

    captchaId = 63048865558
    captchaResponse = 03AOLTBLQOcQXQ3-QTGb0l9Z2b2KnyvjT8-DZCA6zRpNZhUclurh2myXZUoaTI9X55VpRLfeF3hR1Y3XqX5fKcAutEkkU4iIAztI9Q9v52a7wn4J0NVtVqp4YVotFIhQPDvSaEQogh4dh4cdybvWXDxrTHaezqIIUjAAgdw0C3GBAq3bKWh24hIJIfq5G7GWjIT9isCa0DcFLtEHbCZqQVXzdjDbVSx1uWFQzHxjAYkb4kQhM1qGsmFVbtaYz3ehlEtUMND7WMACxESIEQtf5IudEl6ODePgzMXSkuxDSvIFDN3xazYhxiRBfStI83MueCzDlnjcE6idpMhBWRG5A90ur6IXeV-qvMN8UOWoN3M6EUjW98GGI7VYZN_rL0TStf3qJPGDiUZ3qR7RRi0iKNzaMRSITjCbc4NXBlA0Fxqwmo_UY4lGh2XqkSI2_vUidyH3idgcR3tAKqTKOeRC0BLZb0FqdjUXLVw9Nny6aD8kZZNu33lKTy9wJurh7QlycvSgKxhWQH6Uof

  • 相关阅读:
    CSS简要内容
    HTML简要内容
    java中的多线程
    java中的IO流
    hash
    java中的集合
    java中的数组
    java中的异常机制(编译时异常)
    静态变量和静态方法的访问权限
    成员变量和成员方法的访问权限
  • 原文地址:https://www.cnblogs.com/heyus/p/12187444.html
Copyright © 2011-2022 走看看