如何使用Jsoup获取表单validation码图像?
我正在为我的学校网站开发一个应用程序,我正在使用jsoup来解析html。
我遇到了validation码图像的问题我看到了这个问题而且我已经实现但是我没有得到与网站上显示的相同的图像。
如何获得相同的图像validation码,网站使用BotDetectCaptcha我有点困惑我怎么能在我的网站上专门做到这一点
学校网站
正如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);
OUTPUT
在JSoup 1.8.3上测试过
你说你没有得到你在网站上看到的相同图像…这是正常的,因为每次刷新页面时图像都不同。