码迷,mamicode.com
首页 > 编程语言 > 详细

谷歌recaptcha验证码java解决方案

时间:2020-01-13 16:10:17      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:网站   connect   gets   doc   bsp   oat   launcher   vsx   最新版   

 

 

 

 技术图片

 

 

谷歌验证码,好几个版本,此处只针对于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

 

谷歌recaptcha验证码java解决方案

标签:网站   connect   gets   doc   bsp   oat   launcher   vsx   最新版   

原文地址:https://www.cnblogs.com/heyus/p/12187444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!