Java 的 HTTP/HTTPS 请求 java8+,几KB,无三方依赖的http、https请求工具
前言:请求类像是httpclient等框架,很好很优秀,但项目中就有一两个小地方有用到,引入后项目反而体积大了不少,于是 http.java 产生了。它支持常规请求,且体积足够精简!
Java 的 HTTP/HTTPS 请求 适配版本:Java8+ 体积:14KB,无三方依赖,足够精简。 协议:自动适配 http、https 协议的请求 支持get、post、put等请求,以及发送payload
快速使用 1. pom.xml 中加入:
<dependency>
<groupId>cn.zvo.http</groupId>
<artifactId>http</artifactId>
<version>1.5</version>
</dependency> 复制代码 2. Java代码 GET 请求
Response response = new Http().get("http://www.guanleiming.com");
System.out.println(response); 复制代码
POST 请求
Response response = new Http().post("https://www.baidu.com");
System.out.println(response); 复制代码
POST - 发送 payload 请求
Response response = new Http().post("https://www.baidu.com", "payload content", null);
System.out.println(response); 复制代码
自定义请求头 headers
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); 复制代码
put 请求,payload 提交数据
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()); 复制代码
请求时自动携带cookies
Http http = new Http();
http.setCookies("iwSID=0878b2a7-fb1d-44f7-ac58-003e9a68eda7");
Response response = http.get("http://www.guanleiming.com");
System.out.println(response); 复制代码
链接: https://pan.baidu.com/s/1LLF_ec6X5awT5TmcwaMaow
提取码下载: