package nc.hr.utils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
 * @author fenghaifu
 * @version V1.0
 * @Copyright: 上海萃颠信息科技有限公司. All rights reserved.
 * @Title:HttpHelper
 * @projectName data-exchange-platform
 * @Description:通用http请求
 * @date: 2020/1/11 15:16
 * @updatehistory: 2020/1/11 15:16 新增
 */
public class HttpHelper {	
	
	 /**
     * Http post请求
     * @param httpUrl 连接
     * @param param 参数
     * @return
	 * @throws Exception 
     */
    public static String doGet(String httpUrl) throws Exception {
    	initSSL();
        StringBuffer result = new StringBuffer();
        //连接
        HttpURLConnection connection = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br = null;
        //创建连接对象
        URL url = new URL(httpUrl);
        //创建连接
        connection = (HttpURLConnection) url.openConnection();
        //设置请求方法
        connection.setRequestMethod("GET");
        //设置连接超时时间
        connection.setConnectTimeout(10000);
        //设置读取超时时间
        connection.setReadTimeout(10000);
        //DoOutput设置是否向httpUrlConnection输出，DoInput设置是否从httpUrlConnection读入，此外发送post请求必须设置这两个
        //设置是否可读取
        //connection.setDoOutput(true);
        //connection.setDoInput(true);
        //设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
       
        //读取响应
        if (connection.getResponseCode() == 200) {
            is = connection.getInputStream();
            if (null != is) {
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String temp = null;
                while (null != (temp = br.readLine())) {
                    result.append(temp);
                    result.append("\r\n");
                }
            }
        }else {
        	throw new Exception("接口请求失败状态码:"+connection.getResponseCode());
        }
        return result.toString();
    }
    
    
	/*
	 * 跳过SSL
	 */
	private static  void initSSL() throws Exception {
		System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");  
        Security.setProperty("ssl.SocketFactory.provider", "");  
        Security.setProperty("ssl.ServerSocketFactory.provider", "");  
  
        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {  
            @Override  
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
                return null;  
            }  
  
            @Override  
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
            }  
  
            @Override  
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
            }  
        }  
        };  
  
        SSLContext sc = SSLContext.getInstance("TLS");  
        sc.init(null, trustAllCerts, new java.security.SecureRandom());  
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
  
        HostnameVerifier allHostsValid = new HostnameVerifier(){  
            @Override  
            public boolean verify(String hostname, SSLSession session) {  
                return true;  
            }  
        };
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);  
	}
}
