// 创建HttpClient实例 HttpClient httpClient = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(10)) .build(); // 创建HttpRequest对象 HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url) // 目标链接 .header("Content-Type", "application/json") .header("token", "........") // 添加凭证 .POST(HttpRequest.BodyPublishers.ofString(String.valueOf(msg))) .build(); // 发送POST请求并获取响应 try { HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); } catch ( IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); }
JAVA 内部发送 post 请求
最新推荐文章于 2024-07-22 14:15:06 发布