{"id":36417984,"url":"https://github.com/doobo/okhttp-tools","last_synced_at":"2026-01-11T17:01:18.423Z","repository":{"id":37175075,"uuid":"137878556","full_name":"doobo/okhttp-tools","owner":"doobo","description":"POST请求、GET请求、PATCH请求、PUT请求、DELETE请求、上传文件、下载文件、Ws/Wss请求","archived":false,"fork":false,"pushed_at":"2023-06-17T04:14:11.000Z","size":143,"stargazers_count":8,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-18T08:48:36.623Z","etag":null,"topics":["java","okhttp","websocket"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/doobo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-06-19T10:50:07.000Z","updated_at":"2025-08-02T02:31:51.000Z","dependencies_parsed_at":"2025-08-18T08:48:40.848Z","dependency_job_id":"5e0bffeb-9a84-4930-883a-437061cddf35","html_url":"https://github.com/doobo/okhttp-tools","commit_stats":null,"previous_names":["doobo/okhttptools"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/doobo/okhttp-tools","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doobo%2Fokhttp-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doobo%2Fokhttp-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doobo%2Fokhttp-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doobo%2Fokhttp-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doobo","download_url":"https://codeload.github.com/doobo/okhttp-tools/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doobo%2Fokhttp-tools/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28314254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T14:58:17.114Z","status":"ssl_error","status_checked_at":"2026-01-11T14:55:53.580Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","okhttp","websocket"],"created_at":"2026-01-11T17:00:37.381Z","updated_at":"2026-01-11T17:01:18.398Z","avatar_url":"https://github.com/doobo.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OkHttpTools\n\n[![License](https://img.shields.io/badge/license-Apache%202-green.svg)](https://www.apache.org/licenses/LICENSE-2.0)\n\n\u003e 对Okhttp3和RestTemplate进行简单封装,对外提供了POST、GET、PATCH、PUT、DELETE、上传文件、下载文件、取消请求、Ws/Wss请求.\n* 如果异步使用相关JSON解析器，请主动添加该包，默认不会添加，以免引起不必要的引\n* 添加对spring的restTemplate支持,可自定义相关参数，参考文件:okhttp-tools-default.yml,对restTemplate的关系映射使用的是fastjson\n## 如何添加\n```\n \u003cdependency\u003e\n   \u003cgroupId\u003ecom.github.doobo\u003c/groupId\u003e\n   \u003cartifactId\u003eokhttp-tools\u003c/artifactId\u003e\n   \u003cversion\u003e1.3.1\u003c/version\u003e\n \u003c/dependency\u003e\n```\n\n### 在spring项目中使用\n```code\n@RestController\npublic class IndexController {\n\n   /**\n    * 代理URL资源文件\n    */\n    @GetMapping\n    public ResponseEntity\u003cbyte[]\u003e proxyHttp(String url){\n        return RestTemplateUtil.getByClass(url, byte[].class, null);\n    }\n}\n\n@Test\npublic void testGet(){\n    int a = 1;\n    ResponseEntity\u003cString\u003e byClass = RestTemplateUtil.getByClass(\"https://baidu.com?a={a}\", String.class, null, a);\n    System.out.println(byClass.getBody());\n}\n\n@Test\npublic void testPost(){\n    String byClass = RestTemplateUtil.post(\"https://baidu.com\", \"{}\"\n            , new ParameterizedTypeReference\u003cString\u003e(){}, null);\n    System.out.println(byClass);\n}\n```\n### 配置链接池\n```yml\nokhttp:\n  tools:\n    startConfig: true\n    maxIdleConnections: 30\n    keepAliveDuration: 300\n    connectTimeout: 10\n    readTimeout: 20\n    writeTimeout: 10\n```\n\n## OkHttp调用示例\n```code\n//发送get请求\nString result = OkHttpClientTools.getInstance()\n                .get()\n                .addHeader(\"auth\",\"你好\")\n                .url(\"http://www.baidu.com\")\n                .addParam(\"a\", \"B\")\n                .execute().body().string();\n//简单文件上传\nString str = new OkHttpClientTools(client)\n        .upload()\n        .url(\"http://pic.sogou.com/pic/upload_pic.jsp\")\n        .addParam(\"type\",\"utf-8;text/json\")\n        .addFile(\"files\", new File(\"/Users/doobo/Downloads/myAirTicket.png\"))\n        .execute().body().string();\nSystem.out.println(str);\n\n//简单下载文件\nOkHttpClientTools.getInstance()\n    .download()\n    .url(\"http://p4.so.qhmsg.com/t01decceaa40a9f9a19.jpg\")\n    .filePath(\"/Users/doobo/next.jpg\")\n    .tag(this)\n    .enqueue(new DownloadResponseHandler() {\n        @Override\n        public void onStart(long totalBytes) {\n            System.out.println(\"The File is Downing \" + totalBytes);\n        }\n        @Override\n        public void onFinish(File downloadFile) {\n            System.out.println(\"File was Down!\");\n        }\n        @Override\n        public void onProgress(long currentBytes, long totalBytes) {\n            System.out.println(\"doDownload onProgress:\" + currentBytes + \"/\" + totalBytes);\n        }\n        @Override\n        public void onFailure(String error_msg) {\n            countDownLatch.countDown();\n        }\n    });\n\n//302转发兼容\nResponse res = OkHttpClientTools.getInstance()\n                .get()\n                .url(\"https://5fu8.com/m/userCenter.do?agentId=1\u0026vt=5\")\n                .addHeader(\"Cookie\",\"cookiesid=ce3ee9471-27e3-4411-ae94-d89f93dc4ce9-56911233|a3e962802165daacb807c298023a502e\")\n                .execute();\nwhile (res.priorResponse() != null){\n    System.out.println(res.priorResponse().code());\n    res = res.priorResponse();\n}\n\n//webscoket请求实例\nWsBuilder builder = OkHttpClientTools.getInstance().ws()\n                .url(wsUrl)\n                .build();\nWsResponseHandler wsResponseHandler = new WsResponseHandler() {\n    @Override\n    public void onOpen(WebSocket webSocket, Response response) {\n        mWebSocket = builder.getWebSocket();\n        //开启消息定时发送\n        startTask();\n    }\n    public void onMessage(WebSocket webSocket, String text) {\n        //打印一些内容\n    }\n    public void onMessage(WebSocket webSocket, ByteString bytes) {\n    }\n    public void onClosing(WebSocket webSocket, int code, String reason) {\n    }\n    public void onClosed(WebSocket webSocket, int code, String reason) {\n    }\n    public void onFailure(WebSocket webSocket, Throwable t, Response response) {\n        //出现异常会进入此回调\n    }\n};\nbuilder.enqueue(wsResponseHandler);\n```\n## 参考文献\n对于Okhttp3的封装参考了:\n* [https://github.com/hongyangAndroid/okhttputils](https://github.com/hongyangAndroid/okhttputils)\n* [https://github.com/jeasonlzy/okhttp-OkGo](https://github.com/jeasonlzy/okhttp-OkGo)\n* [https://github.com/ZhaoKaiQiang/OkHttpPlus](https://github.com/ZhaoKaiQiang/OkHttpPlus)\n\nLicense\n-------\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoobo%2Fokhttp-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoobo%2Fokhttp-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoobo%2Fokhttp-tools/lists"}