我需要找到一种方法来检查“我的世界”的用户名和密码是否有效。
我找到了这个文档,它告诉了我很多关于“我的世界”身份验证的事情:http://wiki.vg/Authentication
看起来它需要一个JSON HTTP POST请求,但我不知道该怎么做:
我已经搜索了很多,看了很多例子,但这些作品都不是。我得到的最好的结果是控制台中没有打印结果,或者是403错误。
谢谢
发布于 2014-05-05 06:47:52
我知道怎么做了!
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")));https://stackoverflow.com/questions/23457364
复制相似问题