我知道这个问题已在这里被问了几次,但我已经尝试了一切。这可能完全是我的错,因为这是我第一次这样做,但如果是,那么至少我能够改进。
相关代码:
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 Error 411.
有关如何使其发挥作用的任何想法?
提前感谢您的帮助 - Harieo
答案 0 :(得分:1)
在您遇到问题时,只需将connection.getContentLength()
替换为requestData.length
。
TL; DR。
输入setFixedLengthStreamingMode
时,您会发现connected
属性已设置为true
。好吧,何时将其设置为true
?
实际上,already connected
意味着您已经更新了HttpURLConnection,这是根本原因。
您应该检查是否编写了以下代码:
connection.getContentType, connection.getContentEncoding
如果这样做,请给他们评论。此方法的实现使用委托模式,这意味着它们将新建另一个HttpURLConnection。
答案 1 :(得分:0)
connection.setFixedLengthStreamingMode(connection.getContentLength());
问题出在这里。通过调用getContentLength()
,您提交请求并尝试读取响应,然后您尝试设置请求的长度。这没有意义。您需要提供的参数是您要发送的数据量,而不是服务器要发回的数据量。
你还需要写一些东西,再次之前你得到响应代码或做任何意味着获取响应的东西。
正如我所知,连接到我尝试使用的API需要HttpURLConnection.setFixedLengthStreamingMode(),否则它将返回HTTP Error 411。
我看不出你如何判断这段代码。您通过添加setFixedLengthStreamingMode()
完成的所有操作都会更改错误。更有可能的问题是你没有发送任何东西。
注意您需要在调用setDoOutput(true)
后将请求方法设置为PUT ,因为这会将其设置为POST。