Java发起Http远程调用的几种常见方法

在 Java 中,发起 HTTP 远程调用可以使用多种方法,以下是一些常见的方式:

  1. java.net.HttpURLConnection:这是 Java 标准库提供的一种方式,可以通过设置请求方法、请求头和请求体来发送 HTTP 请求,并处理响应。
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class HttpURLConnectionExample {
        public static void main(String[] args) {
            try {
                String url = "http://example.com/api";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    
                // 设置请求方法
                con.setRequestMethod("GET");
    
                int responseCode = con.getResponseCode();
                System.out.println("Response Code : " + responseCode);
    
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
    
                while ((inputLine = in.readLine())!= null) {
                    response.append(inputLine);
                }
                in.close();
    
                System.out.println(response.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  2. Apache HttpClient:这是一个广泛使用的第三方库,提供了更丰富的功能和更方便的 API 来进行 HTTP 操作。

     首先,需要在项目中添加 Apache HttpClient 的依赖:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>

    然后,可以使用以下代码进行 HTTP 请求:

    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;
    
    public class ApacheHttpClientExample {
        public static void main(String[] args) {
            try {
                HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet("http://example.com/api");
    
                HttpResponse response = client.execute(request);
    
                // 处理响应
                //...
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  3. Spring WebClient:如果项目中使用了 Spring Framework,可以使用 WebClient 来进行 HTTP 请求。首先,需要确保项目中引入了 Spring WebFlux 相关依赖。然后,可以使用以下代码进行 HTTP 请求:
import org.springframework.web.reactive.function.client.WebClient;

public class SpringWebClientExample {
    public static void main(String[] args) {
        WebClient webClient = WebClient.create("http://example.com");

        webClient.get()
              .uri("/api")
              .retrieve()
              .bodyToMono(String.class)
              .subscribe(response -> System.out.println(response));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值