setFixedLengthStreamingMode

setFixedLengthStreamingMode

我知道这个问题在这里已经问过几次了,但是我已经尝试了一切。这可能完全是我的错,因为这是我第一次这样做,但如果这样的话,至少我将能够改进。

相关代码:

URL url = new URL(request);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization",
                "Bot (token)");
        connection.setRequestProperty("User-Agent", "https://discordapp.com/developers/docs/reference, 1.0.0");
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        connection.setFixedLengthStreamingMode(connection.getContentLength());

        switch (connection.getResponseCode()) {
        case HttpURLConnection.HTTP_OK:
            break;
        case HttpURLConnection.HTTP_UNAUTHORIZED:
            Executor.logger.warning("Connection to Discord API does not have sufficient authorization!");
            player.sendMessage(ChatColor.RED
                    + "An unrelated error occured in the server, please report this to a staff member.");
            return;
        default:
            Executor.logger.warning(
                    "An unknown exception occured with the HTTP Error Code: " + connection.getResponseCode());
            player.sendMessage(ChatColor.RED + "Linking your accounts has failed!");
            TextComponent message = new TextComponent(ChatColor.AQUA + ChatColor.BOLD.toString() + "click here!");
            message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.mcpz.net"));
            player.sendMessage(
                    ChatColor.YELLOW + "For information on how to find your ID, " + message.toPlainText());
            return;

        }


控制台错误:

    org.bukkit.command.CommandException: Unhandled exception executing command 'link' in plugin DiscordBridge v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:645) ~[squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
    at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
Caused by: java.lang.IllegalStateException: Already connected
    at java.net.HttpURLConnection.setFixedLengthStreamingMode(Unknown Source) ~[?:1.8.0_121]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setFixedLengthStreamingMode(Unknown Source) ~[?:1.8.0_121]
    at uk.co.harieo.DBridge.Discord.Broadcaster.responseMinorUpdate(Broadcaster.java:161) ~[?:?]
    at uk.co.harieo.DBridge.LinkingCommand.onCommand(LinkingCommand.java:39) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[squid-spigot.jar:git-Spigot-4af49dc-c5e9a16]
    ... 15 more


我试过使用connection.disconnet()移动方法,但是它总是返回此异常。正如我发现的那样,必须将HttpURLConnection.setFixedLengthStreamingMode()连接到我尝试使用的API,否则它将返回HTTP错误411。

关于如何使它起作用的任何想法?

感谢您的任何帮助
-哈里欧

最佳答案

connection.setFixedLengthStreamingMode(connection.getContentLength());


问题在这里。通过调用getContentLength(),您将提交请求并尝试读取响应,然后尝试设置请求的长度。这没有道理。您需要提供的参数是要发送多少数据,而不是服务器要发送回多少数据。

您还需要再次编写一些内容,然后才能获得响应代码或执行任何其他隐含获取响应的操作。


正如我发现的那样,必须将HttpURLConnection.setFixedLengthStreamingMode()连接到我尝试使用的API,否则它将返回HTTP错误411。


我看不出如何用此代码分辨。通过添加setFixedLengthStreamingMode()完成的所有操作就是更改错误。问题更可能是您没有发送任何东西。

注意调用setDoOutput(true)后,您需要将请求方法设置为PUT,因为它将请求方法设置为POST。

10-07 22:04