Import URL images in Java from a secure website

I tried to import an image from a website, and when you load the website for the first time, there is some kind of redirect server that allows only browsers. I tried this:

public class TestURLImage {
public static void main(String[] args) throws MalformedURLException, IOException {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            try {
                String path = "https://kissanime.to/Uploads/Etc/12-15-2012/726336142483.jpg";
                System.out.println("Geting Image from " + path);
                System.out.println();    
                URL url = new URL(path);
                BufferedImage image = ImageIO.read(url);
                System.out.println("Loading image into frame...");
                JLabel label = new JLabel(new ImageIcon(image));
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().add(label);
                f.pack();
                f.setLocation(200, 200);
                f.setVisible(true);
            } catch (Exception exp) {
                exp.printStackTrace();
            }

        }
    });

}}

But I get an error message:

Geting Image from https://kissanime.to/Uploads/Etc/12-15-2012/726336142483.jpg    

javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(ImageIO.java:1395)
at javaapplication6.TestURLImage$1.run(TestURLImage.java:35)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://kissanime.to/Uploads/Etc/12-15-2012/726336142483.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1038)
at javax.imageio.ImageIO.read(ImageIO.java:1393)
... 15 more    

So is there any way to get the image? Or at least what's the problem?

+4
source share
1 answer

I suggest you use something more advanced, like apache HTTP Client library . You will have much more control over the entire message. For example, you can install a standard user agent to fake a normal request.

+2
source

Source: https://habr.com/ru/post/1622024/


All Articles