package org.springblade.face.util;

import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;

public class HttpSendPost {

	public static String send(String surl,String sendmsg) throws Exception{
        String res = "";
        HttpURLConnection cnn = null;
        try{
	    	URL url = new URL(surl);
			cnn = (HttpURLConnection) url.openConnection();
			cnn.setUseCaches(false);
			cnn.setDefaultUseCaches(false);
			cnn.setDoInput(true);
			cnn.setDoOutput(true);
			cnn.setRequestMethod("POST");


			cnn.addRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
			cnn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			cnn.setRequestProperty("Connection", "close");
			cnn.setConnectTimeout(5*1000);
			cnn.setReadTimeout(10*1000);
			OutputStream outputStream = cnn.getOutputStream();
			outputStream.write(sendmsg.getBytes("utf-8"));
			outputStream.close();

			if(cnn.getResponseCode() == 200) {
				InputStream in=cnn.getInputStream();
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				byte buf[] = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0){
					out.write(buf, 0, len);
				}
				res = out.toString("UTF-8");
				in.close();
				out.close();
			} else {

				res = String.valueOf(cnn.getResponseCode());
			}
        } catch(Exception e){
        	e.printStackTrace();
        } finally {
        	cnn.disconnect();
        }
        return res;
    }

	public static String send(String surl) throws Exception{
        String res = "";
        HttpURLConnection cnn = null;
        try{
	    	URL url = new URL(surl);
			cnn = (HttpURLConnection) url.openConnection();
			cnn.setUseCaches(false);
			cnn.setDefaultUseCaches(false);
			cnn.setDoInput(true);
			cnn.setDoOutput(true);
			//cnn.setRequestMethod("GET");


			//cnn.addRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
			//cnn.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
			//cnn.setRequestProperty("Connection", "close");
			//cnn.setConnectTimeout(5*1000);
			//cnn.setReadTimeout(10*1000);

			cnn.setRequestMethod("POST");


			cnn.addRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
			cnn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			cnn.setRequestProperty("Connection", "close");
			cnn.setConnectTimeout(5*1000);
			cnn.setReadTimeout(10*1000);

			OutputStream outputStream = cnn.getOutputStream();

			outputStream.close();

			if(cnn.getResponseCode() == 200) {
				InputStream in=cnn.getInputStream();
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				byte buf[] = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0){
					out.write(buf, 0, len);
				}
				res = out.toString("UTF-8");
				in.close();
				out.close();
			} else {

				res = String.valueOf(cnn.getResponseCode());
				//System.out.println("返回错误:"+res);
			}
        } catch(Exception e){
        	e.printStackTrace();
        } finally {
        	cnn.disconnect();
        }
        return res;
    }

	public static BasicHttpClientConnectionManager getCertConn(String certPath, String pwd) {

		try{
			char[] password = pwd.toCharArray();
			FileInputStream certStream = new FileInputStream(new File(certPath));
			KeyStore ks = KeyStore.getInstance("PKCS12");
			ks.load(certStream, password);

			// 实例化密钥库 & 初始化密钥工厂
			KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
			kmf.init(ks, password);

			// 创建 SSLContext
			SSLContext sslContext = SSLContext.getInstance("TLS");
			sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());

			SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
					sslContext,
					new String[]{"TLSv1"},
					null,
					new DefaultHostnameVerifier());
			return new BasicHttpClientConnectionManager(
					RegistryBuilder.<ConnectionSocketFactory>create()
							.register("http", PlainConnectionSocketFactory.getSocketFactory())
							.register("https", sslConnectionSocketFactory)
							.build(),
					null,
					null,
					null
			);
		}catch(Exception e ){
			e.printStackTrace();
		}
		return null ;

	}

}
