抽取一个简单 HTTP 的通用接口,底层实现根据具体引入依赖指定。
<dependency>
<groupId>com.xkcoding.http</groupId>
<artifactId>simple-http</artifactId>
<version>1.0.5</version>
</dependency>
- 默认会按照下面的优先级自行寻找底层实现,
java 11 HttpClient -> OkHttp3 -> apache HttpClient -> hutool-http
- 也可以自行实现
com.xkcoding.http.support.Http
接口,通过HttpUtil.setHttp(new MyHttpImpl())
设置进来 - 可以配置超时时间及代理
HttpUtil.setConfig(HttpConfig.builder()
.timeout(Constants.DEFAULT_TIMEOUT)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
.build());
SimpleHttpResponse response = HttpUtil.get("https://www.google.com");
System.out.println("code = " response.getCode());
System.out.println("body = " response.getBody());