首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Minecraft身份验证

Java Minecraft身份验证
EN

Stack Overflow用户
提问于 2014-05-04 22:26:06
回答 1查看 4.2K关注 0票数 0

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

我找到了这个文档,它告诉了我很多关于“我的世界”身份验证的事情:http://wiki.vg/Authentication

看起来它需要一个JSON HTTP POST请求,但我不知道该怎么做:

我已经搜索了很多,看了很多例子,但这些作品都不是。我得到的最好的结果是控制台中没有打印结果,或者是403错误。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-05-05 06:47:52

我知道怎么做了!

代码语言:javascript
复制
    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;
    }

使用方法:

代码语言:javascript
复制
System.out.println(httpRequest(new URL("https://authserver.mojang.com/authenticate"), MakeJSONRequest("YourUsername", "YourPassword")));
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23457364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档