美文网首页
Https工具类 支持GET PUT POST DELETE

Https工具类 支持GET PUT POST DELETE

作者: jsjack_wang | 来源:发表于2018-01-05 12:02 被阅读0次
    1.RequestMethodEnum枚举
    public enum RequestMethodEnum {
        GET("GET"),
        POST("POST"),
        PUT("PUT"),
        DELETE("DELETE");
    
        private String methodName;
    
        RequestMethodEnum(String methodName) {
            this.methodName = methodName;
        }
    
        public String getMethodName() {
            return methodName;
        }
    
        public void setMethodName(String methodName) {
            this.methodName = methodName;
        }
    }
    
    2.MyX509TrustManager用于https请求
    import javax.net.ssl.X509TrustManager;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    public class MyX509TrustManager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
        }
    
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
        }
    
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
    
    3.HttpsUtil工具类
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import java.io.*;
    import java.net.URL;
    
    public final class HttpsUtil {
    
        private HttpsUtil() { }
    
        public static String httpsRequest(String targetUrl, RequestMethodEnum methodEnum,
                                          String accessToken, String outputData) throws Exception {
            StringBuilder sb = new StringBuilder();
            try {
                TrustManager[] tm = {new MyX509TrustManager()};
                SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
                sslContext.init(null, tm, new java.security.SecureRandom());
                SSLSocketFactory ssf = sslContext.getSocketFactory();
    
                URL url = new URL(targetUrl);
                HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
    
                httpUrlConn.setRequestProperty("Content-Type", "application/json");
                httpUrlConn.setRequestProperty("Accept", "application/json");
                httpUrlConn.setRequestProperty("Charset", "UTF-8");
                if (accessToken != null && !"".equals(accessToken)) {
                    httpUrlConn.setRequestProperty("access-token", accessToken);
                }
                httpUrlConn.setRequestProperty("Charset", "UTF-8");
                httpUrlConn.setSSLSocketFactory(ssf);
    
                httpUrlConn.setDoOutput(true);
                httpUrlConn.setDoInput(true);
                httpUrlConn.setUseCaches(false);
    
                httpUrlConn.setRequestMethod(methodEnum.getMethodName());
                if (RequestMethodEnum.GET == methodEnum) {
                    httpUrlConn.connect();
                }
                if (outputData != null && !"".equals(outputData)) {
                    OutputStream outputStream = httpUrlConn.getOutputStream();
                    outputStream.write(outputData.getBytes("UTF-8"));
                    outputStream.close();
                }
    
                InputStream inputStream = httpUrlConn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
                String partResult;
                while ((partResult = bufferedReader.readLine()) != null) {
                    sb.append(partResult);
                }
    
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();
                httpUrlConn.disconnect();
            } catch (Exception e) {
                throw new Exception("网络请求出错");
            }
            return sb.toString();
        }
    }
    
    4.测试
        public static void main(String[] args) throws Exception {
    //        String request = httpsRequest("url",
    //                RequestMethodEnum.GET, "47f02c9d51724879a93d8ac27e6ef71a", null);
    //        System.out.println(request);
    
    //        String request = httpsRequest("url",
    //                RequestMethodEnum.POST, "47f02c9d51724879a93d8ac27e6ef71a", null);
    //        System.out.println(request);
    
    //        String request = httpsRequest("url",
    //                RequestMethodEnum.DELETE, "47f02c9d51724879a93d8ac27e6ef71a", null);
    //        System.out.println(request);
    
            String request = httpsRequest("url",
                    RequestMethodEnum.PUT, "47f02c9d51724879a93d8ac27e6ef71a", null);
            System.out.println(request);
        }

    相关文章

      网友评论

          本文标题:Https工具类 支持GET PUT POST DELETE

          本文链接:https://www.haomeiwen.com/subject/fhgjnxtx.html