Https工具类 支持GET PUT POST DELETE

2018-01-05  本文已影响0人  jsjack_wang
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);
    }
上一篇下一篇

猜你喜欢

热点阅读