public Image getImage(HttpCookie cookie) throws Exception { // 1. Administrar cookies CookieManager manager = new CookieManager(); CookieHandler.setDefault(manager); CookieStore cookieJar = manager.getCookieStore(); // 2. Connect HttpsURLConnection connection = null; System.setProperty("https.protocols", "TLSv1,SSLv3,SSLv2Hello"); URL url = new URL(URL_IMAGE); connection = (HttpsURLConnection) url.openConnection(); // 3. Inject cookie cookieJar.add(url.toURI(), cookie); // 4. Read image BufferedInputStream reader = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); int c; while ((c = reader.read()) != -1) { byteArrayOut.write(c); }
Image image = Toolkit.getDefaultToolkit().createImage(byteArrayOut.toByteArray()); reader.close(); return image; } System.setProperty("https.protocols", "TLSv1,SSLv3,SSLv2Hello"); UserAgent userAgent = new UserAgent(); userAgent.visit(URL_IMAGE); objUserAgent.cookies.addCookie(cookie);
System.out.println(objUserAgent.doc);
message: UserAgent.sendGET; response is of unsupported content type (image/jpeg)
public Image getCaptcha(Cookie cookie) throws Exception { UserAgent userAgent = new UserAgent(); HandlerForBinary handlerForBinary = new HandlerForBinary(); userAgent.setHandler("image/jpeg", handlerForBinary); userAgent.cookies.addCookie(cookie); userAgent.visit(URL_IMAGE); System.out.println(handlerForBinary.getContent().length); Image image = Toolkit.getDefaultToolkit().createImage(handlerForBinary.getContent()); return image; }