java post请求设置httpentry
时间: 2023-04-13 17:00:54 浏览: 74
Java中设置HTTP请求的HTTPEntry可以使用以下代码:
```
URL url = new URL("http://example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
String jsonInputString = "{\"name\": \"John\", \"age\": 30}";
OutputStream os = conn.getOutputStream();
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, , input.length);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
```
其中,设置HTTP请求方法为POST,设置请求体的内容类型为JSON格式,设置请求体的内容为jsonInputString。最后通过输入流获取响应结果。