Java http 接口请求详解

Java 中进行 HTTP 接口请求的方式有多种,常用的方式包括使用 Java 原生的 HttpURLConnection 类、Apache HttpClient 库和 Spring 的 RestTemplate。

  1. 使用 HttpURLConnection 类进行 HTTP 接口请求:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class HttpUrlConnectionExample {
        public static void main(String[] args) {
            try {
                // 创建 URL 对象
                URL url = new URL("http://example.com/api/endpoint");
    
                // 打开连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
                // 设置请求方法
                connection.setRequestMethod("GET");
    
                // 发送请求
                int responseCode = connection.getResponseCode();
    
                // 读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
    
                // 关闭连接
                connection.disconnect();
    
                // 处理响应
                System.out.println("Response Code: " + responseCode);
                System.out.println("Response Body: " + response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  2. 使用 Apache HttpClient 库进行 HTTP 接口请求:

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class ApacheHttpClientExample {
        public static void main(String[] args) {
            try {
                // 创建 HttpClient 对象
                HttpClient httpClient = HttpClients.createDefault();
    
                // 创建 HttpGet 请求对象
                HttpGet httpGet = new HttpGet("http://example.com/api/endpoint");
    
                // 发送请求并获取响应
                HttpResponse response = httpClient.execute(httpGet);
    
                // 读取响应
                HttpEntity entity = response.getEntity();
                String responseBody = EntityUtils.toString(entity);
    
                // 处理响应
                System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
                System.out.println("Response Body: " + responseBody);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  3. 使用 Spring 的 RestTemplate 进行 HTTP 接口请求(需要添加相关依赖):

    import org.springframework.http.ResponseEntity;
    import org.springframework.web.client.RestTemplate;
    
    public class RestTemplateExample {
        public static void main(String[] args) {
            try {
                // 创建 RestTemplate 对象
                RestTemplate restTemplate = new RestTemplate();
    
                // 发送 GET 请求并获取响应
                ResponseEntity<String> response = restTemplate.getForEntity("http://example.com/api/endpoint", String.class);
    
                // 处理响应
                System.out.println("Response Code: " + response.getStatusCode());
                System.out.println("Response Body: " + response.getBody());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值