我在我的Java程序中创建了一个复杂的Json对象。我尝试请求 Http Post,但响应代码是 415,我希望看到响应代码为 200,因为 200 表示可以。谁能帮我解决这个问题?
String post_url = "https://candidate.hubteam.com/candidateTest/v3/problem/result?userKey=1cae96d3904b260d06d0daa7387c";
URL obj = new URL(post_url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String urlParameters = res.toString();
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
// the following is an example about how did I build the complicated Json object
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name","Sam");
jsonObject1.put("age",7);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name","David");
jsonObject2.put("age",10);
List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
jsonObjects.add(jsonObject1);
jsonObjects.add(jsonObject2);
jsonObject.put("fans",jsonObjects);
我希望看到响应代码为 200,而不是 415
最佳答案
我相信您想将其放入您的代码中! 如果您注意到在连接中定义了 http 方法,则可以更改为使用 setRequestMethod 进行发布
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
关于java - Http Post的响应代码是415。如何使响应代码为200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540346/