OKHttpUtil搞定Http请求
一、OKHttpUtil 功能
- 根据URL自动判断是请求HTTP还是HTTPS,不需要单独写多余的代码。
- 默认情况下Cookie自动记录,比如可以实现模拟登录,即第一次访问登录URL后后续请求就是登录状态。
- 自动识别304跳转并二次请求
- 支持代理配置
- 支持referer配置
- 支持User-Agent配置
- 自动识别并解压Gzip格式返回内容
- 支持springboot 配置文件
- 极简的封装调用
二、OKHttpUtil使用
1、 maven引入
1 2 3 4 5 | <dependency> <groupId>io.github.admin4j</groupId> <artifactId>http</artifactId> <version>0.4.0</version> </dependency> |
2、GET请求
最简单的使用莫过于用HttpUtil工具类快速请求某个接口:
1 2 | Response response = HttpUtil. get ( "https://github.com/search" , Pair.of( "q" , "okhttp" )); System. out .println( "response = " + response); |
3、POST请求
一行代码即可搞定,当然Post请求也很简单:
1 2 3 4 5 6 7 8 9 10 11 12 | # JSON 格式的body Response post = HttpUtil.post( "https://oapi.dingtalk.com/robot/send?access_token=27f5954ab60ea8b2e431ae9101b1289c138e85aa6eb6e3940c35ee13ff8b6335" , "{\"msgtype\": \"text\",\"text\": {\"content\":\"【反馈提醒】我就是我, 是不一样的烟火\"}}" ); System. out .println( "post = " + post); # form 请求 Map<String, Object> formParams = new HashMap<>(16); formParams.put( "username" , "admin" ); formParams.put( "password" , "admin123" ); Response response = HttpUtil.postForm( "http://192.168.1.13:9100/auth/login" , formParams ); System. out .println( "response = " + response); |
返回格式为JSON的 可以使用 HttpJsonUtil
自动返回JsonObject
1 2 3 4 | JSONObject object =HttpJsonUtil. get ( "https://github.com/search" , Pair.of( "q" , "http" ), Pair.of( "username" , "agonie201218" )); System. out .println( "object = " + object ); |
4、文件上传
1 2 3 4 5 6 7 | File file= new File( "C:\\Users\\andanyang\\Downloads\\Sql.txt" ); Map<String, Object> formParams= new HashMap<>(); formParams.put( "key" , "test" ); formParams.put( "file" ,file); formParams.put( "token" , "WXyUseb-D4sCum-EvTIDYL-mEehwDtrSBg-Zca7t:qgOcR2gUoKmxt-VnsNb657Oatzo=:eyJzY29wZSI6InpoYW56aGkiLCJkZWFkbGluZSI6MTY2NTMwNzUxNH0=" ); Response response=HttpUtil.upload( "https://upload.qiniup.com/" ,formParams); System. out .println(response); |
5、下载文件
1 | HttpUtil.down( "https://gitee.com/admin4j/common-http" , "path/" ); |
6、HttpRequest 链式请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # get Response response=HttpRequest. get ( "https://search.gitee.com/?skin=rec&type=repository" ) .queryMap( "q" , "admin4j" ) .header(HttpHeaderKey.USER_AGENT, "admin4j" ) .execute(); System. out .println( "response = " +response); # post form Response response=HttpRequest. get ( "http://192.168.1.13:9100/auth/login" ) .queryMap( "q" , "admin4j" ) .header(HttpHeaderKey.USER_AGENT, "admin4j" ) .form( "username" , "admin" ) .form( "password" , "admin123" ) .execute(); System. out .println( "response = " +response); |
本文来自博客园,作者:橘子偏爱橙子,转载请注明原文链接:https://www.cnblogs.com/xfbk/p/17050980.html
分类:
JAVA-自动化系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2022-01-13 【性能测试】Tps和响应时间之间的关系