前言:请求类像是httpclient等框架,很好很优秀,但项目中就有一两个小地方有用到,引入后项目反而体积大了不少,于是 http.java 产生了。它支持常规请求,且体积足够精简!
- 适配版本:Java8+
- 体积:14KB,无三方依赖,足够精简。
- 协议:自动适配 http、https 协议的请求
- 支持get、post、put等请求,以及发送payload
<!-- http/https请求工具 https://github.com/xnx3/http.java -->
<dependency>
<groupId>cn.zvo.http</groupId>
<artifactId>http</artifactId>
<version>1.5</version>
</dependency>
Response response = new Http().get("http://www.guanleiming.com");
System.out.println(response);
Response response = new Http().post("https://www.baidu.com");
System.out.println(response);
Response response = new Http().post("https://www.baidu.com", "payload content", null);
System.out.println(response);
Map<String, String> headers = new HashMap<String, String>(); //请求头
headers.put("Content-Type", "application/x-www-form-urlencoded");
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
Response response = new Http().post("https://www.baidu.com", "payload content", headers);
System.out.println(response);
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=123"; //请求url
String payload = "payload content"; //提交的 payload 数据内容
Response response = new Http().put(url, payload, null);
System.out.println(response.getContent());
Http http = new Http();
http.setCookies("iwSID=0878b2a7-fb1d-44f7-ac58-003e9a68eda7");
Response response = http.get("http://www.guanleiming.com");
System.out.println(response);