import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpPostExample { public static void main(String[] args) throws IOException { String url = "https://api2.hik-cloud.com/oauth/token"; String postData = "{\"client_id\":\"874a183dbb49413389da36a5604bebff\",\"client_secret\":\"a979232b74044de0aa0c12ee2a8bcc43\",\"grant_type\":\"client_credentials\"}"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST con.setRequestMethod("POST"); // 启用输出流,用于发送POST数据 con.setDoOutput(true); // 设置POST数据 try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { wr.writeBytes(postData); wr.flush(); } // 获取响应码 int responseCode = con.getResponseCode(); System.out.println("Response Code: " + responseCode); // 读取响应内容 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } System.out.println("Response: " + response.toString()); } } }这段代码结果D:\jdk1.8\bin\java.exe "-javaagent:D:\IDEA\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar=52938:D:\IDEA\IntelliJ IDEA 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\jdk1.8\jre\lib\charsets.jar;D:\jdk1.8\jre\lib\deploy.jar;D:\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\jdk1.8\jre\lib\ext\cldrdata.jar;D:\jdk1.8\jre\lib\ext\dnsns.jar;D:\jdk1.8\jre\lib\ext\jaccess.jar;D:\jdk1.8\jre\lib\ext\jfxrt.jar;D:\jdk1.8\jre\lib\ext\localedata.jar;D:\jdk1.8\jre\lib\ext\nashorn.jar;D:\jdk1.8\jre\lib\ext\sunec.jar;D:\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\jdk1.8\jre\lib\ext\zipfs.jar;D:\jdk1.8\jre\lib\javaws.jar;D:\jdk1.8\jre\lib\jce.jar;D:\jdk1.8\jre\lib\jfr.jar;D:\jdk1.8\jre\lib\jfxswt.jar;D:\jdk1.8\jre\lib\jsse.jar;D:\jdk1.8\jre\lib\management-agent.jar;D:\jdk1.8\jre\lib\plugin.jar;D:\jdk1.8\jre\lib\resources.jar;D:\jdk1.8\jre\lib\rt.jar;F:\javaprojecct\pro01\out\production\pro01 HttpPostExample Response Code: 200 Response: Process finished with exit code 0
时间: 2023-07-30 22:09:59 浏览: 240
根据你提供的代码,HTTP POST请求已成功发送,并且响应码为200,表示请求成功。然而,从你的输出结果中可以看出,响应内容为空。这可能是因为你没有正确处理响应流。在这种情况下,你可以尝试使用`getErrorStream()`方法来获取响应流,即使响应码为200,也可能会获得有用的错误信息。修改代码如下所示:
```java
// 读取响应内容
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println("Response: " + response.toString());
} catch (IOException e) {
// 如果获取错误流成功,则读取错误信息
try (BufferedReader errorIn = new BufferedReader(new InputStreamReader(con.getErrorStream()))) {
String errorLine;
StringBuilder errorResponse = new StringBuilder();
while ((errorLine = errorIn.readLine()) != null) {
errorResponse.append(errorLine);
}
System.out.println("Error Response: " + errorResponse.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
```
通过添加对错误流的处理,你可以查看是否存在任何错误信息。如果仍然无法获得响应内容,请检查API端点的返回数据是否符合预期,并确保你的请求参数和数据格式正确。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)