Get content type (image/jpeg)

43 views
Skip to first unread message

Luis Coronel

unread,
Jul 25, 2018, 2:13:58 PM7/25/18
to jaunt-api
Hi,

I got a cookie from https://cel.reniec.gob.pe/valreg/valreg.do with Jaunt.
I will use this cookie to get a image (from this URL: https://cel.reniec.gob.pe/valreg/codigo.do).

Without jaunt I did this:
    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;
    }

I am trying it with Jaunt but I don't find a manner to get image (can I define content type?)

        System.setProperty("https.protocols", "TLSv1,SSLv3,SSLv2Hello");
        UserAgent userAgent = new UserAgent();
        userAgent.visit(URL_IMAGE);
        objUserAgent.cookies.addCookie(cookie);
        System.out.println(objUserAgent.doc);

As additional information I got this message:
message: UserAgent.sendGET; response is of unsupported content type (image/jpeg)

Thanks.

Tom Cervenka

unread,
Jul 25, 2018, 4:24:03 PM7/25/18
to jaunt-api
Here's an example of downloading images and other non-html content-types:

Luis Coronel

unread,
Jul 25, 2018, 5:40:13 PM7/25/18
to jaunt-api
Thanks, it's done:
    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;
    }


Reply all
Reply to author
Forward
0 new messages