HTTP 请求库 java-requests
3月21日 深圳 OSC 源创会开始报名罗,送华为海思开发板
Java的世界里,HttpClient 是一个功能强大的Http请求库,然而接口非常复杂,设计上遵从正交性,简单的请求也需要写比较多的代码,更不要说隐藏在各种细节里面的高级用法了。
Requests, 是一个模仿python requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。
简单的请求示例:
String url = ...;Response<String> 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 bytesResponse<byte[]> resp1 = Requests.get(url).bytes();// save response as file Response<File> resp2 = Requests.get(url).file("/path/to/save/file");// url 参数:Map<String, Object> map = new HashMap<>();map.put("k1", "v1");map.put("k2", "v2");Response<String> resp = Requests.get(url).param("key1", "value1").params(map) //.params(new Parameter(...), new Parameter(...)) .text();// 请求头Response<String> resp = Requests.get(url).header("key1", "value1").headers(map) //.headers(new Header(...), new Header(...)) .text();// 添加Cookie:Map<String, Object> cookies = new HashMap<>();Response<String> resp = Requests.get(url).cookie("key1", "value1").cookies(map) //.cookies(new Cookie(...), new Cookie(...)) .text();// 设置 userAgentResponse<String> resp = Requests.get(url).userAgent(userAgent).text();// 增加请求数据(post, put, patch方法)// send form-encoded data. x-www-form-urlencoded header will be send automaticallyResponse<String> resp = Requests.post(url).data(map).text();// send string dataString str = ...;resp = Requests.post(url).data(str, "UTF-8").text();// send from inputStreamInputStream in = ...resp = Requests.post(url).data(in).text();// multipart 请求, 用于文件上传:Response<String> resp = Requests.post(url).data(map).multiPart("ufile", "/path/to/file") .multiPart(..., ...).text();
请求设置:
//禁止自动重定向Response<String> resp = Requests.get(url).allowRedirects(false).text();//超时// both connec timeout, and socket timeoutResponse<String> resp = Requests.get(url).timeout(30_1000).text();// set connect timeout and socket timeoutresp = Requests.get(url).timeout(30_1000, 30_1000).text();//禁止使用gzipResponse<String> resp = Requests.get(url).gzip(false).text();// 不检查https 证书Response<String> resp = Requests.get(url).verify(false).text();// Http Basic 验证Response<String> resp = Requests.get(url).auth("user", "passwd").verify(false).text();//代理,支持http, https, socks 代理Response<String> 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<String> resp1 = session.get(url1).text();Response<String> resp2 = session.get(url2).text();
连接池. 连接池可以用来在多个请求之间复用Http 连接:
ConnectionPool connectionPool = ConnectionPool.custom().verify(false) .maxPerRoute(20) .maxTotal(100) //.proxy(...) .build();Response<String> resp1 = Requests.get(url1).connectionPool(connectionPool).text();Response<String> resp2 = Requests.get(url2).connectionPool(connectionPool).text();connectionPool.close();
如果使用了连接池,verify 和 proxy 需要在连接池中设置,Requests 中的设置无效。