java - 方法不允许错误 (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();
解决方案
在实例中设置请求方法HttpURLConnection
,默认值为GET
.
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
推荐阅读
- git - 如何使用 git 合并来自不同存储库的拉取请求(或提交)?
- python - 我收到一条错误消息,提示类别匹配查询不存在
- user-interface - HiveMQ 中 Web UI 的 SSL/TLS
- docker - Hadoop namenode 无法识别 docker 中的数据节点
- laravel - Vagrant Share - Laravel Homestead,Ubuntu
- android-studio - Android Studio 没有按属性对齐我的代码
- java - Jooq Loggerlistener 查询执行时间不一致
- azure-cosmosdb - 在 ARM 模板中获取 CosmosDb 主连接字符串
- windows-10 - Windows 10 上的“bash:fork:重试:资源暂时不可用”错误
- jenkins - 如何让通用 Webhook 触发器插件与 Jenkins 中的多分支管道一起使用?