简体   繁体   中英

Method not allowed error (405) Mojang API

´

I am currently working on a project where I am trying to log the player in through the mojang API but it returns an error (405) Method not allowed (seems like it somehow thinks I'm sending a GET request instead of a POST)

Would be glad if anyone could help me out.

Here is the source code for the auth request:

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;
    }
}

Note: I have already done a lot of debugging and the provided credentials are working and they're not formatted wrong.

Fix: I have added the following lines

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

Set the request method in HttpURLConnection instance, default value is GET .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM