RESTAPI可以从Postman调用,显示来自Java的400个错误请求
我正在尝试使用POST-restful服务。当我在邮递员身上试用时,我得到了成功的回复。但当我用下面的代码在java上尝试时,我得到的响应代码是400。在《邮递员》中,我用替换转义字符粘贴相同的输入
try {
URL url = new URL("https://localhost/PasswordVault/api/Accounts");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\", \"detail\":{\"ver\":\"2020\",\"productionDate\":\"2020\"}}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (Exception e) {
System.out.println("Exception:- " + e);
}
# 1 楼答案
首先,检查邮递员可能正在发送的请求头,而您的代码没有。在发送请求之前,您可能需要在请求中添加一些标题。其次,我强烈建议使用一些3d party Http客户端库,而不是自己编写Http请求。一些著名的库是Apache Http client和OK Http client。我个人使用自己的Http客户端,它是MgntUtils开源库的一部分。你也可以试试。以下是简化的代码,仅用于演示如何从网站上读取一些响应:
当然,您也可以使用这个库发送POST请求、设置请求头、读取文本或二进制响应和响应头。这里是HttpClient class Javadoc的链接。该库本身以Maven artifact和Github的形式提供,包括Javadoc和源代码
# 2 楼答案
我强烈建议你使用图书馆来做这件事。 您可以使用Jackson,这将极大地简化和标准化java与远程HTTP服务的交互
以下几点会让你开始:
# 3 楼答案
一些想法:
"post data".getBytes(StandardCharsets.UTF_8)
connection.getErrorStream()
)例如