import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import java.io.BufferedReader; import java.io.InputStreamReader; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet getRequest = new HttpGet("http://api.example.com/data"); try { HttpResponse response = httpClient.execute(getRequest); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; public class OkHttpExample { public static void main(String[] args) { OkHttpClient httpClient = new OkHttpClient(); Request request = new Request.Builder() .url("http://api.example.com/data") .build(); try { Response response = httpClient.newCall(request).execute(); System.out.println(response.body().string()); } catch (IOException e) { e.printStackTrace(); } } } import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class RestTemplateExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.getForEntity("http://api.example.com/data", String.class); System.out.println(response.getBody()); } }


上一篇:
下一篇:
切换中文