gpt4 book ai didi

Java Minecraft 身份验证

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

我需要找到一种方法来检查我的世界用户名和密码是否有效。

我发现这个文档讲述了很多有关 Minecraft 身份验证的事情:http://wiki.vg/Authentication

看起来它需要一个 JSON HTTP POST 请求,但我不知道如何做到这一点:S

我进行了很多搜索并查看了很多示例,但这些都不起作用。我得到的最好结果是控制台中没有打印结果或出现 403 错误。

谢谢

最佳答案

我知道怎么做了!

    private static String MakeJSONRequest(String username, String password){
JSONObject json1 = new JSONObject();
json1.put("name", "Minecraft");
json1.put("version", 1);
JSONObject json = new JSONObject();
json.put("agent", json1);
json.put("username", username);
json.put("password", password);

return json.toJSONString();
}

private static String httpRequest(URL url, String content) throws Exception {
byte[] contentBytes = content.getBytes("UTF-8");

URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(contentBytes.length));

OutputStream requestStream = connection.getOutputStream();
requestStream.write(contentBytes, 0, contentBytes.length);
requestStream.close();

String response = "";
BufferedReader responseStream;
if (((HttpURLConnection) connection).getResponseCode() == 200) {
responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
} else {
responseStream = new BufferedReader(new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8"));
}

response = responseStream.readLine();
responseStream.close();

if (((HttpURLConnection) connection).getResponseCode() != 200) {
//Failed to login (Invalid Credentials or whatever)
}

return response;
}

如何使用它:

System.out.println(httpRequest(new URL("https://authserver.mojang.com/authenticate"), MakeJSONRequest("YourUsername", "YourPassword")));

关于Java Minecraft 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23457364/

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