【文件属性】:
文件名称:HTTP请求库java-requests.zip
文件大小:78KB
文件格式:ZIP
更新时间:2022-08-05 06:20:43
开源项目
Java的世界里,HttpClient 是一个功能强大的Http请求库,然而接口非常复杂,设计上遵从正交性,简单的请求也需要写比较多的代码,更不要说隐藏在各种细节里面的高级用法了。Requests, 是一个模仿python requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。
简单的请求示例:String url = ...;
Response resp = Requests.get(url).text();
// post 和其他方法
resp = Requests.post(url).text();
resp = Requests.head(url).text();
//读取Http Response
int statusCode = resp.getStatusCode();
Headers headers = resp.getHeaders();
Cookies cookies = resp.getCookies();
String body = resp.getBody();
//response 返回其他类型
resp = Requests.get(url).text("UTF-8");
// get response as bytes
Response resp1 = Requests.get(url).bytes();
// save response as file
Response resp2 = Requests.get(url).file("/path/to/save/file");
// url 参数:
Map map = new HashMap<>();
map.put("k1", "v1");
map.put("k2", "v2");
Response resp = Requests.get(url).param("key1", "value1").params(map)
//.params(new Parameter(...), new Parameter(...))
.text();
// 请求头
Response resp = Requests.get(url).header("key1", "value1").headers(map)
//.headers(new Header(...), new Header(...))
.text();
// 添加Cookie:
Map cookies = new HashMap<>();
Response resp = Requests.get(url).cookie("key1", "value1").cookies(map)
//.cookies(new Cookie(...), new Cookie(...))
.text();
// 设置 userAgent
Response resp = Requests.get(url).userAgent(userAgent).text();
// 增加请求数据(post, put, patch方法)
// send form-encoded data. x-www-form-urlencoded header will be send automatically
Response resp = Requests.post(url).data(map).text();
// send string data
String str = ...;
resp = Requests.post(url).data(str, "UTF-8").text();
// send from inputStream
InputStream in = ...
resp = Requests.post(url).data(in).text();
// multipart 请求, 用于文件上传:
Response resp = Requests.post(url).data(map).multiPart("ufile", "/path/to/file")
.multiPart(..., ...).text();请求设置://禁止自动重定向
Response resp = Requests.get(url).allowRedirects(false).text();
//超时
// both connec timeout, and socket timeout
Response resp = Requests.get(url).timeout(30_1000).text();
// set connect timeout and socket timeout
resp = Requests.get(url).timeout(30_1000, 30_1000).text();
//禁止使用gzip
Response resp = Requests.get(url).gzip(false).text();
// 不检查https 证书
Response resp = Requests.get(url).verify(false).text();
// Http Basic 验证
Response resp = Requests.get(url).auth("user", "passwd").verify(false).text();
//代理,支持http, https, socks 代理
Response resp = Requests.get("http://www.baidu.com/")
.proxy(Proxy.httpProxy("127.0.0.1", 8080))
.text();Session. session 可以用来维持http 会话,自动处理cookie, basic auth 等信息:Session session = Requests.session();
Response resp1 = session.get(url1).text();
Response resp2 = session.get(url2).text();连接池. 连接池可以用来在多个请求之间复用Http 连接:ConnectionPool connectionPool = ConnectionPool.custom().verify(false)
.maxPerRoute(20)
.maxTotal(100)
//.proxy(...)
.build();
Response resp1 = Requests.get(url1).connectionPool(connectionPool).text();
Response resp2 = Requests.get(url2).connectionPool(connectionPool).text();
connectionPool.close();如果使用了连接池,verify 和 proxy 需要在连接池中设置,Requests 中的设置无效。
标签:javarequests