简体   繁体   中英

403 Error When Trying To Access an Image URL

First of all, i should mention that i tried to find a fix on the internet for 2 days or even more, so if you want to link me to another post, i've most likely seen it already. Now for the problem:

When i try to download images, with some links my code works and with some it will give an error, for example, this link gives an error(along with all the other links i try from cdn.discordapp.com): https://cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg

Before you look at the code i should also mention that it uses an external api, but still you'll probably understand most of it, also, my URL is always an image URL.

This is my code:

   try {
    url = new URL(event.getMessage().getContent().substring(12));
    openConnection = url.openConnection();
    String userCredentials = "username:password";
    String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
    openConnection.setRequestProperty ("Authorization", basicAuth);
    openConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    openConnection.setRequestProperty("Request Method", "POST");
//  openConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    openConnection.setRequestProperty("Content-Language", "en-US");
    openConnection.setUseCaches(false);
    openConnection.setDoInput(true);
    openConnection.setDoOutput(true);
    if(openConnection.getContentLength() > 8000000) {
        event.getChannel().sendMessage(event.getAuthor().getAsMention()+" file size is too big.").queue();
        check = false;
    }
} catch (Exception e) {
    event.getChannel().sendMessage("Couldn't create a connection to the link, please recheck the link.").queue();
    check = false;
    e.printStackTrace();
}
if(check) {
    BufferedImage img = null;
    try {
        InputStream in = new BufferedInputStream(url.openStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        while (-1 != (n=in.read(buf)))
        {
            out.write(buf, 0, n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();
        img = ImageIO.read(new ByteArrayInputStream(response));
    } catch (Exception e) {
        event.getChannel().sendMessage(event.getAuthor().getAsMention()+" couldn't read an image from this link.").queue();
        e.printStackTrace();
    }
    JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
    jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpegParams.setCompressionQuality(0.1f);
    File file = new File("output.jpg");
    try {
        final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(new FileImageOutputStream(file));
        writer.write(null, new IIOImage(img, null, null), jpegParams);
        event.getChannel().sendFile(file, null).queue();
        file.delete();
    } catch (IOException e) {
        event.getChannel().sendMessage("Couldn't create/send the output image.").queue();
        e.printStackTrace();
    }
}

And this is the stack trace with the error (You can probably ignore anything from this line [23:21:06] [Fatal] [JDA]: One of the EventListeners had an uncaught exception and down):

`    java.io.IOException: Server returned HTTP response code: 403 for URL: https://cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at discordBot.Jpegify.execute(Jpegify.java:73)
at discordBot.Christina.onMessageReceived(Christina.java:163)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:316)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:64)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:129)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:51)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:665)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:339)
at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)
[23:21:06] [Fatal] [JDA]: One of the EventListeners had an uncaught exception
[23:21:06] [Fatal] [JDA]: Encountered an exception:
[23:21:06] [Fatal] [JDA]: java.lang.IllegalArgumentException: image == null!
at javax.imageio.IIOImage.<init>(Unknown Source)
at discordBot.Jpegify.execute(Jpegify.java:99)
at discordBot.Christina.onMessageReceived(Christina.java:163)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:316)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:64)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:129)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:51)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:665)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:339)
at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)`

Of course any help is greatly appreciated, and sorry for the long post, this problem is just really getting on my nerves...

I figured it out. You need to do a couple things. First you need to set the user agent on the URLConnection. Then you need to openConnection on the URLConnection. Finally, where you are previously getting stream from the URL, you actually need to getInputStream from the URLConnection because that is where you have set the user agent.

Here is the code I have and it works. Copy and paste it into a new file called Test.java and try running it. Note I commented out the file delete so you can actually see the file downloads

Using it in conjunction with your Events and Channels now is up to you, good luck.

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;

public class Test {
    public static void main(String[] args) throws MalformedURLException, IOException {
        URL url = new URL("http://cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg");
        URLConnection openConnection = url.openConnection();
        boolean check = true;

        try {

            openConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            openConnection.connect();

            if (openConnection.getContentLength() > 8000000) {
                System.out.println(" file size is too big.");
                check = false;
            }
        } catch (Exception e) {
            System.out.println("Couldn't create a connection to the link, please recheck the link.");
            check = false;
            e.printStackTrace();
        }
        if (check) {
            BufferedImage img = null;
            try {
                InputStream in = new BufferedInputStream(openConnection.getInputStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int n = 0;
                while (-1 != (n = in.read(buf))) {
                    out.write(buf, 0, n);
                }
                out.close();
                in.close();
                byte[] response = out.toByteArray();
                img = ImageIO.read(new ByteArrayInputStream(response));
            } catch (Exception e) {
                System.out.println(" couldn't read an image from this link.");
                e.printStackTrace();
            }
            JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
            jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(0.1f);
            File file = new File("output.jpg");
            try {
                final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
                writer.setOutput(new FileImageOutputStream(file));
                writer.write(null, new IIOImage(img, null, null), jpegParams);
                // event.getChannel().sendFile(file, null).queue();
                // file.delete();
            } catch (IOException e) {
                System.out.println("Couldn't create/send the output image.");
                e.printStackTrace();
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM