英文:
Method not allowed error (405) Mojang API
问题
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 while 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();
英文:
´
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();
</details>
# 答案1
**得分**: 0
在`HttpURLConnection`实例中设置请求方法,默认值为`GET`。
```java
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
英文:
Set the request method in HttpURLConnection
instance, default value is GET
.
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论