如何使用 Spring mvc 发送请求?
How to send request with Spring mvc?
我正在学习如何制作电报机器人。使用 webhook 电报向您发送 post 请求和一些消息。
我希望我的服务器获取此请求,处理并将新请求发送到电报 get/post。
例如:
telegram bot 向我发送了一个 post 请求,其中包含聊天中的新消息。
我收到这个请求并处理它。
现在我需要发送新的 get 请求到电报 post 像 https://api.telegram.org/bot/sendMessage?chat_id=&text=hello
这样的消息
有没有办法直接从控制器发送请求?我知道我可以重定向请求,但重定向只能是 GET,我需要完全新的请求。
您可以使用 Rest Template to send request to a web service.this 是关于如何操作的 spring 指南。
您可以使用 Spring 的 RestTemplate:
RestTemplate restTemplate = new RestTemplate();
UriComponentsBuilder telegramRequestBuilder = UriComponentsBuilder.fromHttpUrl("https://api.telegram.org/bot/sendMessage")
.queryParam("chat_id", 1)
.queryParam("text", "Hello");
ResponseEntity<String> response
= restTemplate.getForEntity(telegramRequestBuilder.toUriString(), String.class); // or to a Java pojo class
或者为此使用较新的 Spring WebClient。例如,参见 this link。
我正在学习如何制作电报机器人。使用 webhook 电报向您发送 post 请求和一些消息。 我希望我的服务器获取此请求,处理并将新请求发送到电报 get/post。 例如: telegram bot 向我发送了一个 post 请求,其中包含聊天中的新消息。 我收到这个请求并处理它。 现在我需要发送新的 get 请求到电报 post 像 https://api.telegram.org/bot/sendMessage?chat_id=&text=hello
这样的消息有没有办法直接从控制器发送请求?我知道我可以重定向请求,但重定向只能是 GET,我需要完全新的请求。
您可以使用 Rest Template to send request to a web service.this 是关于如何操作的 spring 指南。
您可以使用 Spring 的 RestTemplate:
RestTemplate restTemplate = new RestTemplate(); UriComponentsBuilder telegramRequestBuilder = UriComponentsBuilder.fromHttpUrl("https://api.telegram.org/bot/sendMessage") .queryParam("chat_id", 1) .queryParam("text", "Hello"); ResponseEntity<String> response = restTemplate.getForEntity(telegramRequestBuilder.toUriString(), String.class); // or to a Java pojo class
或者为此使用较新的 Spring WebClient。例如,参见 this link。