写一个springboot 调用httpclient的例子
时间: 2023-03-06 18:27:33 浏览: 126
spring boot中使用http请求的示例代码
答:以下是一个简单的Spring Boot应用程序,它使用Apache HttpClient来调用HTTP请求:@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
} @Autowired
private RestTemplateBuilder restTemplateBuilder; @Bean
public RestTemplate restTemplate() {
return restTemplateBuilder.build();
} public String sendGetRequest() throws Exception {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://www.example.com");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
return result.toString();
}
}
阅读全文