小编use*_*361的帖子

尝试访问图像 URL 时出现 403 错误

首先,我应该提到我试图在互联网上找到修复程序 2 天或更长时间,所以如果你想把我链接到另一个帖子,我很可能已经看到了。现在的问题:

当我尝试下载图像时,有些链接我的代码有效,有些链接会出错,例如,此链接出错(以及我从 cdn.discordapp.com 尝试的所有其他链接):https:/ /cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg

在您查看代码之前,我还应该提到它使用外部 api,但您仍然可能会理解其中的大部分内容,此外,我的 URL 始终是图像 URL。

这是我的代码:

   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 …
Run Code Online (Sandbox Code Playgroud)

java url image download

2
推荐指数
1
解决办法
2079
查看次数

标签 统计

download ×1

image ×1

java ×1

url ×1