【发布时间】:2018-06-11 13:16:23
【问题描述】:
在minecraft authentication page中,用户发送一个特定格式的https post请求以获得响应,我相信我已经正确使用了。但是,我收到 Method Not Allowed 错误,这意味着服务器收到的不是发布请求,这是不正确的。
服务器变量:
final String server = "https://authserver.mojang.com/authenticate";
这是我的有效载荷的发送方式:
Gson gson = new Gson();
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(server);
StringEntity postingString = new StringEntity(gson.toJson(payload));
post.setEntity(postingString);
post.setHeader("content-type", "application/json");
HttpResponse response = (HttpResponse) httpClient.execute(post);
this.authResponse = response.toString();
这是我收到的新的完整响应消息:
HttpResponseProxy{HTTP/1.1 400 Bad Request [Content-Type: application/json, Content-Length: 655, Connection: keep-alive, Accept-Ranges: bytes, Date: Mon, 01 Jan 2018 15:28:36 GMT, Server: Restlet-Framework/2.3.10, Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept, X-Cache: Error from cloudfront, Via: 1.1 6046e14e05d9aeb3ea5b4dd85091e9a6.cloudfront.net (CloudFront), X-Amz-Cf-Id: 4wciQ7Mp6OExfWnU3C9frmifhqWQT5_vpQRvUbXPrDy-O-nJqPWtWQ==] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 655,Chunked: false]}}
我的请求内容如下:
{
"agent": {
"name": "Minecraft",
"version": 1
},
"username": "my username",
"password": "my password"
}
更清晰的错误响应消息:
{"error":"JsonMappingException","errorMessage":"Can not construct instance of com.mojang.yggdrasil.auth.dataaccess.memcached.throttling.captcha.CaptchaCredentials: no String-argument constructor/factory method to deserialize from String value ('{\n \"agent\": { \n \"name\": \"Minecraft\", \n \"version\": 1 \n \n },\n \"username\": \"my username\", \n \n \"password\": \"my password\"\n}')\n at [Source: HttpInputOverHTTP@5e25d803; line: 1, column: 1]"}
【问题讨论】:
-
"表示服务器收到的不是 post 请求" 不。它确实收到了 POST 请求,但这个端点只接受 GET请求。
-
@Ivar 我不太明白你的意思,那么我将如何将我的 json 内容提交到服务器?
-
文档确实谈到了使用 POST 请求。需要
405 Method Not Allowed才能返回带有允许的请求方法的“允许”标头。在您的情况下,它只允许 GET。也许您使用了错误的端点? -
谢谢,我更改了端点。但是,由于某种原因,我现在收到
Bad Request,请检查我编辑的代码。 -
但您似乎过于频繁地调用此函数,您现在需要使用验证码。它在文档中没有说明任何内容。你可以过一段时间再试。之后它可能会正常工作。