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...