如何使用Jsoup获取表单验证码图像? [英] How to get a form captcha image with Jsoup?

查看:1126
本文介绍了如何使用Jsoup获取表单验证码图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我遇到了验证码的问题我看到<一个href =https://stackoverflow.com/questions/28619161/getting-captcha-image-using-jsoup>这个问题,我已经实现了,但我没有得到与网站。



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



解决方案

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



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

  //加载获取所需cookie的初始页面
Connection conn = Jsoup.connect(https:// www.saes.upiicsa.ipn.mx/);
文件d = conn.get();

元素captcha = d.select(#c_default_ctl00_leftcolumn_loginuser_logincaptcha_CaptchaImage)。first();
if(captcha == null){
抛出新的RuntimeException(无法找到验证码......);
}

//获取验证码图片
Connection.Response response = Jsoup //
.connect(captcha.absUrl(src))//提取image绝对URL
.cookies(conn.response()。cookies())//抓取cookie
.ignoreContentType(true)//获取图像需要
.execute();

//从Jsoup响应加载图像
ImageIcon image = new ImageIcon(ImageIO.read(new ByteArrayInputStream(response.bodyAsBytes())));

//显示图片
JOptionPane.showMessageDialog(null,image,Captcha image,JOptionPane.PLAIN_MESSAGE);

输出





在JSoup 1.8.3上测试


I am developing an application for my school website and I'm using jsoup for parsing the html.

I'm facing a problem with captcha image I see this question and I had implemented but I am not getting the same image as is showed in the website.

How can I get the same image captcha, the website is using BotDetectCaptcha I am a little confused how can I do it specifically on my website

School Website

解决方案

As stated in SLaks comment, you may be missing some cookies.

Here is a working example with the provided 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);

OUTPUT

Tested on JSoup 1.8.3

这篇关于如何使用Jsoup获取表单验证码图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆