Jam*_*t20 0 java authentication post login minecraft
我需要找出一种方法来检查我的用户名和密码是否有效.
我发现这个文档讲述了很多关于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;
}
Run Code Online (Sandbox Code Playgroud)
如何使用它 :
System.out.println(httpRequest(new URL("https://authserver.mojang.com/authenticate"), MakeJSONRequest("YourUsername", "YourPassword")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2976 次 |
| 最近记录: |