Java HttpsURLConnection 发送 Https请求
1 package com.xxxx; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.net.URL; 9 import java.security.KeyManagementException; 10 import java.security.NoSuchAlgorithmException; 11 import java.security.SecureRandom; 12 import java.security.cert.CertificateException; 13 import java.security.cert.X509Certificate; 14 import java.util.Map; 15 16 import javax.net.ssl.HostnameVerifier; 17 import javax.net.ssl.HttpsURLConnection; 18 import javax.net.ssl.KeyManager; 19 import javax.net.ssl.SSLContext; 20 import javax.net.ssl.SSLSession; 21 import javax.net.ssl.SSLSocketFactory; 22 import javax.net.ssl.TrustManager; 23 import javax.net.ssl.X509TrustManager; 24 25 import org.apache.log4j.Logger; 26 27 /** 28 * Https 请求工具类 29 * 30 * @Author 31 * @Date 2020-7-13 13:58:15 32 */ 33 public class HongliHttpsUtil { 34 35 private static Logger log = Logger.getLogger(HongLiQPSyncClient.class); 36 37 /** 38 * 发送 https get请求 39 * 40 * @param uri 41 * @return 42 * @throws IOException 43 * @Author 44 * @Date 2020-7-13 15:14:12 45 */ 46 public static String doGet(String uri, String token, Mapparams){ 47 HttpsURLConnection httpsConn; 48 try { 49 httpsConn = getHttpsURLConnection(uri + setUrlParams(params), "GET"); 50 httpsConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//text/plain 51 httpsConn.setRequestProperty("ACCESS_TOKEN",token); 52 byte[] bytes = getBytesFromStream(httpsConn.getInputStream()); 53 return new String(bytes); 54 } catch (IOException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 return null; 59 } 60 61 /** 62 * 发送 https post请求 63 * 64 * @param uri 65 * @param data 66 * @return 67 * @throws IOException 68 * @Author 69 * @Date 2020-7-13 15:14:35 70 */ 71 public static String doPost(String uri, String data, Map params){ 72 HttpsURLConnection httpsConn; 73 try { 74 httpsConn = getHttpsURLConnection(uri + setUrlParams(params), "POST"); 75 httpsConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//text/plain 76 setBytesToStream(httpsConn.getOutputStream(),data.getBytes()); 77 byte[] bytes = getBytesFromStream(httpsConn.getInputStream()); 78 return new String(bytes); 79 } catch (IOException e) { 80 e.printStackTrace(); 81 } 82 83 return null; 84 } 85 86 private static String setUrlParams(Map params) { 87 StringBuilder sb = new StringBuilder("?"); 88 int i = 0; 89 for(Map.Entry param: params.entrySet()) { 90 String paramName = param.getKey(); 91 String paramValue = param.getValue(); 92 log.info(paramName + ": " + paramValue); 93 sb.append(paramName + "=" + paramValue); 94 if (i ) { 95 sb.append("&"); 96 } 97 i++; 98 } 99 return new String(sb); 100 } 101 102 /** 103 * 获取HttpsURLConnection链接 104 * 105 * @param uri 请求地址 106 * @param method 请求方法(POST GET) 107 * @return 108 * @throws IOException 109 * @Author 110 * @Date 2020-7-13 15:16:42 111 */ 112 private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException { 113 SSLContext ctx = null; 114 try { 115 ctx = SSLContext.getInstance("TLS");//TLS SSLv3 116 ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); 117 } catch (KeyManagementException e) { 118 e.printStackTrace(); 119 } catch (NoSuchAlgorithmException e) { 120 e.printStackTrace(); 121 } 122 SSLSocketFactory ssf = ctx.getSocketFactory(); 123 124 URL url = new URL(uri); 125 HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection(); 126 httpsConn.setSSLSocketFactory(ssf); 127 httpsConn.setHostnameVerifier(new HostnameVerifier() { 128 @Override 129 public boolean verify(String arg0, SSLSession arg1) { 130 return true; 131 } 132 }); 133 httpsConn.setRequestMethod(method); 134 httpsConn.setDoInput(true); 135 httpsConn.setDoOutput(true); 136 httpsConn.setRequestProperty("Accept","*/*"); 137 httpsConn.setRequestProperty("Connection", "keep-alive"); 138 httpsConn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"); 139 return httpsConn; 140 } 141 142 /** 143 * 从请求的输入流中获取数据并返回字节数组 144 * 145 * @param is 146 * @return 147 * @throws IOException 148 * @Author 149 * @Date 2020-7-13 15:15:45 150 */ 151 private static byte[] getBytesFromStream(InputStream is) throws IOException { 152 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 153 byte[] kb = new byte[1024]; 154 int len; 155 while ((len = is.read(kb)) != -1) { 156 baos.write(kb, 0, len); 157 } 158 byte[] bytes = baos.toByteArray(); 159 baos.close(); 160 is.close(); 161 return bytes; 162 } 163 164 /** 165 * post请求 发送参数 166 * 167 * @param os 168 * @param bytes 169 * @throws IOException 170 * @Author 171 * @Date 2020-7-13 15:15:02 172 */ 173 private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException { 174 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 175 byte[] kb = new byte[1024]; 176 int len; 177 while ((len = bais.read(kb)) != -1) { 178 os.write(kb, 0, len); 179 } 180 os.flush(); 181 os.close(); 182 bais.close(); 183 } 184 185 } 186 187 final class DefaultTrustManager implements X509TrustManager { 188 189 @Override 190 public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { 191 } 192 193 @Override 194 public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { 195 } 196 197 @Override 198 public X509Certificate[] getAcceptedIssuers() { 199 return null; 200 } 201 202 }
原文链接:https://blog.csdn.net/Zjruana/article/details/81488866