Java 将JSON负载部署到服务器
我试图对需要JSON负载的服务器进行API调用,并返回一个Java 将JSON负载部署到服务器,java,json,json-simple,Java,Json,Json Simple,我试图对需要JSON负载的服务器进行API调用,并返回一个 //Prereq: username/password are string variables, this.url is a URL() HttpURLConnection http = (HttpURLConnection) this.url.openConnection(); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type", "applic
//Prereq: username/password are string variables, this.url is a URL()
HttpURLConnection http = (HttpURLConnection) this.url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json");
http.setUseCaches(false);
http.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(http.getOutputStream())) {
wr.writeBytes(this.getPayload(username, password).toJSONString());
wr.flush();
}
StringBuilder sb = new StringBuilder();
//Error here - returned 400.
try (InputStreamReader is = new InputStreamReader(http.getInputStream()); BufferedReader br = new BufferedReader(is)) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
JSONObject response = (JSONObject) JSONValue.parse(sb.toString());
getPayload()
返回我想在web请求中发送的JSONObject,但是服务器返回一个400
HTTP状态错误,表明我设置的某些内容一定是错误的
如何正确地将/POST
JSONObject输出到web服务器
资源:
{
"agent": {
"name":"Minecraft",
"version":1
},
"password":"---",
"clientToken":cf6ffbc6-a681-47c1-9999-119c6d2ed597,
"username":"---"
}
发现了我自己的愚蠢问题,负载没有将
UUID
作为字符串写入。和HTTP错误代码400
表示错误请求,这是非常普遍的。。。你没有关于你的请求有什么问题的更多信息吗?在我看来,您实际上是在发送一个JSON负载,服务器应该理解这确实是一个JSON负载。如果不是这样,我希望服务器返回HTTP错误415
,用于不支持的媒体type@ccjmne那么你认为代码没有本质上的错误吗400实际上是通用的,所以我不能完全确定到底是哪里出了问题。我将添加一个关于协议的链接,我最初忘记了包括它。