首页 > 解决方案 > 方法不允许错误 (405) Mojang API

问题描述

'

我目前正在做一个项目,我试图通过 mojang API 登录播放器,但它返回错误 (405) Method not allowed(似乎它以某种方式认为我正在发送 GET 请求而不是 POST)

如果有人可以帮助我,我会很高兴。

以下是身份验证请求的源代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONException;
import org.json.JSONObject;
import net.sxlver.accountchecker.exception.AccessDeniedException;
import net.sxlver.accountchecker.manager.OutputManager;
public class AuthRequest {
   
    private OutputManager outputManager = new OutputManager();
   
    /**
    * 
    * @param username
    * @param password
    * @return required JSON Object containing the credentials and a few other information the API needs as String
    * @throws JSONException if JSONObject contains invalid data
    */
   
    public String MakeJSONRequest(String username, String password) throws JSONException {
        JSONObject json1 = new JSONObject();
        json1.put("name", "Minecraft");
        json1.put("version", "1");
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);
       
        return json.toString();
    }
   
    /**
    * 
    * @param url
    * @param content
    * @return the API's response as String (JSONObject)
    * @throws AccessDeniedException if the provided credentials are invalid
    * @throws IOException if any issues are encountered whilest preparing and/or sending the request
    * @throws JSONException 
    */
   
    public boolean httpRequest(URL url, String content) throws AccessDeniedException, IOException, JSONException {
        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));
       
        String response = "";
        BufferedReader responseStream;
        if(((HttpsURLConnection) connection).getResponseCode() == 200) {
            responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        }else {
            responseStream = new BufferedReader(new InputStreamReader(((HttpsURLConnection) connection)
                    .getErrorStream(), "UTF-8"));
        }
       
        response = responseStream.readLine();
        responseStream.close();
       
        if(((HttpsURLConnection) connection).getResponseCode()!=200) {
            JSONObject json = new JSONObject();
            try {
                json = new JSONObject(content);
            } catch (JSONException e) {
                System.out.println("Error: Invalid JSON request. Could not parse content to JSONObject.");
                return false;
            }
            outputManager.print("Access denied for " + json.get("username") + ":" + json.get("password") 
            + ". Response code: " + ((HttpsURLConnection) connection).getResponseCode());
            return false;
        }
       
        return true;
    }
}

注意:我已经做了很多调试,并且提供的凭据正在工作,并且它们的格式没有错误。

修复:我添加了以下几行

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

标签: java

解决方案


在实例中设置请求方法HttpURLConnection,默认值为GET.

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");

推荐阅读