本文介绍了Java的认证的Minecraft的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要找出一种方法来检查的Minecraft用户名和密码是否有效。
I need to figure out a way to check if minecraft username and password is valid.
我发现这个文档是讲述一个关于认证的Minecraft很多事情:
I have found this documentation which is telling a lot of things about the minecraft authentication : http://wiki.vg/Authentication
看起来像它需要一个JSON HTTP POST请求,但我不知道该怎么做:•
Looks like it needs a JSON HTTP POST Request but I have no idea how to do that :S
我寻觅了很多,经历了很多为例但这些作品中去。最好的结果我已经是或控制台打印没有结果403错误。
I have searched a lot and went through a lot of exemple but none of these works. The best result I had is no result printed in console or a 403 error.
感谢
推荐答案
我想通了,该怎么办呢!
I figured out how to do it !
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!