gpt4 book ai didi

java - IllegalStateException : Already connected with HttpURLConnection. setFixedLengthStreamingMode

转载 作者:行者123 更新时间:2023-12-01 08:52:01 24 4
gpt4 key购买 nike

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

相关代码:

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。

关于如何让它发挥作用有什么想法吗?

感谢您提前提供的任何帮助- Harieo

最佳答案

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

TL;博士。

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

事实上,已经连接意味着你重新新建了HttpURLConnection,这才是根本原因。

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

connection.getContentType, connection.getContentEncoding

如果您这样做,请发表评论。该方法的实现使用委托(delegate)模式,这意味着它们将新建另一个 HttpURLConnection。

关于java - IllegalStateException : Already connected with HttpURLConnection. setFixedLengthStreamingMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com