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

How to get a form captcha image with Jsoup?

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

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

我怎样才能得到相同的图像验证码,该网站正在使用 BotDetectCaptcha 我有点困惑我怎么才能在我的网站上专门做到这一点

School Website

您说您得到的图片与您在网站上看到的图片不一样... 这很正常,因为每次刷新页面时图像都不一样。

如 SLaks 评论所述,您可能缺少一些 cookie。

这是一个工作示例,其中提供了 url:

// 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);

输出

在 JSoup 1.8.3 上测试