Java IllegalStateException:已与HttpURLConnection.setFixedLengthStreamingMode连接

Java IllegalStateException:已与HttpURLConnection.setFixedLengthStreamingMode连接,java,http,Java,Http,我知道这个问题在这里已经被问过好几次了,但我已经试过了。这可能完全是我的错,因为这是我第一次这样做,但如果是这样的话,至少我能够改进 相关代码: URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization",

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

相关代码:

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()移动这些方法,但它总是返回此异常。我发现,连接到我尝试使用的API需要HttpURLConnection.setFixedLengthStreamingMode(),否则它将返回HTTP错误411

你有什么办法让它发挥作用吗

提前谢谢你的帮助 -哈里奥

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

在获取响应代码或执行任何其他暗示获取响应的操作之前,您还需要编写一些内容

我发现,连接到我尝试使用的API需要HttpURLConnection.setFixedLengthStreamingMode(),否则它将返回HTTP错误411

我不明白你怎么能用这个代码来判断。添加
setFixedLengthStreamingMode()
只需更改错误即可。更可能的问题是你没有发送任何东西


注意:在调用
setDoOutput(true)
之后,您需要将请求方法设置为PUT,因为这会将其设置为POST。

在您的问题下,只需将
connection.getContentLength()
替换为
requestData.length

TL;博士

当您进入
setFixedLengthStreamingMode
时,您会发现
connected
属性已设置为
true
。那么,什么时候设置为
true

事实上,
已连接
意味着您重新连接了HttpURLConnection,这是根本原因

您应该检查是否编写了以下代码:

connection.getContentType, connection.getContentEncoding

如果你这样做,请评论他们。此方法的实现使用委托模式,这意味着它们将创建另一个HttpURLConnection。

在不调用服务器响应的情况下,如何准确获取内容长度?设置固定长度流模式时不需要内容长度。我已经解释过了。参数是请求的长度,而不是响应的长度。也许我使用的术语“内容”是错误的,我问的是如何找到方法所需的参数,与服务器响应无关。再一次,我已经告诉过你:“你需要提供的参数是你要发送多少数据”…再一次,我询问是否有一种方法可以在不遇到原始问题的情况下确定我发送了多少数据。
connection.getContentType, connection.getContentEncoding