Java HttpsUnits 工具类实现 Https
前言
本文简记 Java 的 HttpsUnits 工具类。
Java 中实现 Http 访问网络很简单,但是实现 Https 相对来说比较复杂。
HttpsUnits.java
-
码云: HttpsUnits.java 。
-
概述:
getFile()
和getFileByte()
只实现了通过POST
请求读取网络文件并写入文件,没有实现了GET
。
复制
import ...... /** * HttpsUnits 工具类 */ public class HttpsUnits { private static final int TIMEOUT = 45000; public static final String ENCODING = "UTF-8"; /** * 创建 http 连接 * 设置 https 请求,信任所有 https 证书,绕过 https 的 SSL 验证 * * @param url 地址 * @param method 方法 * @param headerParameters 头信息 * @param body 请求内容 * @return HttpURLConnection httpConnection * @throws Exception 异常抛出 */ private static HttpURLConnection createConnection(String url, String method, Map<String, String> headerParameters, String body) throws Exception {} /** * POST请求 * * @param address 请求地址 * @param headerParameters 参数 * @param body * @return proxyHttpRequest() <- inputStream2String() <- StringBuilder.toString() * @throws Exception 异常抛出 */ public static String post(String address, Map<String, String> headerParameters, String body) throws Exception {} /** * GET请求 * * @param address URL地址 * @param headerParameters * @param body * @return proxyHttpRequest() <- inputStream2String() <- StringBuilder.toString() * @throws Exception 异常抛出 */ public static String get(String address, Map<String, String> headerParameters, String body) throws Exception {} /** * 读取网络文件并写入文件 * * @param address URL地址 * @param headerParameters // * @param body * @param file File对象 * @return readInputStream() <- "success" * @throws Exception 异常抛出 */ public static String getFile(String address, Map<String, String> headerParameters, File file) throws Exception {} /** * 以 Byte 读取网络文件并写入文件 * * @param address String * @param headerParameters ap<String, String> * @return byte[] * @throws Exception 异常抛出 */ public static byte[] getFileByte(String address, Map<String, String> headerParameters, File file) throws Exception {} /** * 读取文件流并写入文件 * * @param in InputStream对象 * @return "success" * @throws Exception 异常抛出 */ public static String readInputStream(InputStream in, File file) throws Exception {} /** * 以 Byte 读取文件流并写入文件 * * @param in InputStream * @return byte[] * @throws Exception 抛出异常 */ public static byte[] readInputStreamToByte(InputStream in, File file) throws Exception {} /** * HTTP 请求 * * @param address 地址 * @param method 方法 * @param headerParameters 头信息 * @param body 请求内容 * @return inputStreamToString() <- StringBuilder.toString() * @throws Exception 异常抛出 */ public static String proxyHttpRequest(String address, String method, Map<String, String> headerParameters, String body) throws Exception {} /** * 将参数化为 body,调用 getRequestBody(Map<String, String> params, boolean urlEncode) * * @param params Map<String, String>对象 * @return getRequestBody() <- StringBuilder.toString() */ public static String getRequestBody(Map<String, String> params) {} /** * 将参数化为 body 方法 * * @param params Map<String, String>对象 * @param urlEncode boolean urlEncode * @return StringBuilder.toString() */ public static String getRequestBody(Map<String, String> params, boolean urlEncode) {} /** * 读取 InputStream 到 string * * @param input InputStream对象 * @param encoding String encoding * @return StringBuilder.toString() * @throws IOException 异常抛出 */ private static String inputStreamToString(InputStream input, String encoding) throws IOException {} /** * 设置 https 请求,信任所有 https 证书,绕过 https 的 SSL 验证 * * @throws Exception 异常抛出 */ private static void trustAllHttpsCertificates() throws Exception {} /** * 设置 https 请求证书 */ static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {} }
测试
-
示例:
复制
import java.io.File; import java.util.HashMap; import java.util.Map; public class main { public static void main(String[] args) { try { // get 请求 //请求地址(这里使用一言的接口) System.out.println("Get:\n"); String address_get = "https://v1.hitokoto.cn/"; //请求参数 Map<String, String> params_get = new HashMap<>(); params_get.put("c", "d");//这是该接口需要的参数 params_get.put("encode", "text");//这是该接口需要的参数 // 调用 get 请求 String res = HttpsUnits.get(address_get, params_get, null); System.out.println(res);//打印返回参数 System.out.println("\n\nPost:\n"); //请求地址(这里使用淘宝提供的手机号码信息查询的接口) String address_post = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm"; //请求参数 Map<String, String> params_post = new HashMap<>(); params_post.put("tel", "13777777777");//这是该接口需要的参数 // 调用 post 请求 String res_post = HttpsUnits.post(address_post, params_post, null); System.out.println(res_post);//打印返回参数 System.out.println("\n\nFile Post Write...\n"); File file_post = new File("src/file/file_post.txt"); HttpsUnits.getFile(address_post, params_post, file_post); System.out.println("\n\nFile Post Write...\n"); File file_post_byte = new File("src/file/file_post_byte.txt"); HttpsUnits.getFileByte(address_post, params_post, file_post_byte); } catch (Exception e) { // TODO 异常 e.printStackTrace(); } } }
-
显示:
复制
Get: 用我一生,换你十年天真无邪。 Post: __GetZoneResult_ = { mts:'1377777', province:'浙江', catName:'中国移动', telString:'13777777777', areaVid:'30510', ispVid:'3236139', carrier:'浙江移动'} File Post Write... File Post Write...
作者:Yogile
出处:https://www.cnblogs.com/Yogile/p/13273007.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构