如何使用Jsoup获取表单验证码图像?

时间:2015-12-27 20:26:04

标签: java android jsoup captcha

我正在为我的学校网站开发一个应用程序,我正在使用jsoup来解析html。

我遇到验证码图片的问题我看到this问题我已经实施了但是我没有得到与网站上显示的图像相同的图像。

如何获得相同的图片验证码,网站正在使用 BotDetectCaptcha 我有点困惑我怎么能在我的网站上专门做到这一点

School Website

enter image description here

2 个答案:

答案 0 :(得分:8)

如SLaks评论中所述,您可能会错过一些Cookie。

以下是提供的网址的工作示例:

// Load the initial page for getting the required cookies
Connection conn = Jsoup.connect("https://www.saes.upiicsa.ipn.mx/");
Document d = conn.get();

Element captcha = d.select("#c_default_ctl00_leftcolumn_loginuser_logincaptcha_CaptchaImage").first();
if (captcha == null) {
    throw new RuntimeException("Unable to find captcha...");
}

// Fetch the captcha image
Connection.Response response = Jsoup //
        .connect(captcha.absUrl("src")) // Extract image absolute URL
        .cookies(conn.response().cookies()) // Grab cookies
        .ignoreContentType(true) // Needed for fetching image
        .execute();

// Load image from Jsoup response
ImageIcon image = new ImageIcon(ImageIO.read(new ByteArrayInputStream(response.bodyAsBytes())));

// Show image
JOptionPane.showMessageDialog(null, image, "Captcha image", JOptionPane.PLAIN_MESSAGE);

<强>输出

enter image description here

在JSoup 1.8.3上测试

答案 1 :(得分:1)

你说你没有得到你在网站上看到的相同图像...... 这是正常的,因为每次刷新页面时图像都不同。

相关问题