java - Http Post的响应代码是415。如何使响应代码为200

标签 java json http-post

我在我的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/

相关文章:

testing - Jmeter POST 请求未正确处理

java - http post 不是通过代码工作,而是通过 postman 工作

forms - ASP MVC 3 RAZOR动态表单生成贴

java - 在日期属性为字符串的列表中查找具有最大日期的对象

java - MenuListener 实现,如何检测点击了哪个 JMenu?

java - 在库中使用 realm.io 时出现 NoSuchMethodError

java - 如何按插入时的顺序遍历所有 json 值?

java - 如何用 Java 编写五的求和

javascript - 迭代 javascript 对象

asp.net-mvc - 如何使用 MVC 在 ASP.NET 上应用 MVVM?