# http.java
**Repository Path**: m163/http.java
## Basic Information
- **Project Name**: http.java
- **Description**: java8+,几KB,无三方依赖的http、https请求工具
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 7
- **Created**: 2025-04-16
- **Last Updated**: 2025-04-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
前言:请求类像是httpclient等框架,很好很优秀,但项目中就有一两个小地方有用到,引入后项目反而体积大了不少,于是 http.java 产生了。它支持常规请求,且体积足够精简!
# Java 的 HTTP/HTTPS 请求
* 适配版本:Java8+
* 体积:14KB,无三方依赖,足够精简。
* 协议:自动适配 http、https 协议的请求
* 支持get、post、put等请求,以及发送payload
## 快速使用
#### 1. pom.xml 中加入:
````
cn.zvo.http
http
1.2
````
#### 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 headers = new HashMap(); //请求头
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);
````