package com.jkcredit.invoice.util;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.*;
import java.util.Iterator;

/**
 * Created by zhangqingxin Date : 16/11/6 Time : 10:41
 * <p/>
 * 参考一(HttpClient)：http://mvnrepository.com/artifact/commons-httpclient/commons-
 * httpclient/3.1
 * 参考一(json-lib)：http://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4
 */
public class QueryDemo_Test {

	/**
	 * 测试地址
	 */
		private static final String URL = "http://127.0.0.1:18081/gateway?api=credit.sec.data";
	 	//private static final String URL = "http://110.88.150.74:80/credit?api=credit.sec.data_test";
	  	//private static final String URL = "http://110.88.150.74/credit?api=credit.sec.data";
		//private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data_test";
		//private static final String URL = "http://123.57.186.204/gateway?api=credit.sec.data";
		//private static final String URL = "http://www1.h11.site/gateway?api=credit.sec.data";
	 	//private static final String URL = "http://110.88.150.68:8000/gateway?api=credit.sec.data";
		//private static final String URL = "http://60.205.114.163:8000/gateway?api=credit.sec.data";
	 	//private static final String URL = " http://45.126.120.88/gateway?api=credit.sec.data"; 
		//private static final String URL = "http://119.18.195.163/gateway?api=credit.sec.data";

	/**
	 * 分配的appKey
	 */
	 	//private static final String appKey = "junxin_test";
	
		private static final String appKey = "rongchengzhiyun";
	 	//private static final String appKey = "ccx";

	/**
	 * 方法入口
	 *
	 * @param args
	 */
	public static void main(String args[]) throws Exception {
		   QueryDemo_Test demo = new QueryDemo_Test();
		demo.runMainService();
	}

	/**
	 * 调用主接口
	 */
	public void runMainService() {
		try {
			/**
			 * 实名demo
			 */
			JSONObject paramJsonObj = initParamJsonObj_Realname();
			String ret = postClient(URL, paramJsonObj);
			System.out.println(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 整合参数(实名认证)
	 *
	 * @return
	 */
	public JSONObject initParamJsonObj_Realname() {
		/**
		 * 基本参数
		 */
		JSONObject paramJsonObj = new JSONObject();
		paramJsonObj.put("api", "PROTOCOL_ADD_V1");//CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
		//paramJsonObj.put("api", "MSISDNMD5TOIMEI");//MOBILE_CHECK_V1//CMCC_3RD_VERIFY_V4//CTCC_CHECK_V1
		paramJsonObj.put("appKey", appKey);
	    paramJsonObj.put("appSecret",
		"DDEF5E6D6028FCA74144290AF7EA628A9DE84B39");
		//"84C1CE24EDF361F28072E313BD87EAB24CC727CF");
	    
	   

		/**
		 * 业务参数
		 */
		JSONObject dataJson = initMainServiceParamJsonObj_Realname();
		paramJsonObj.put("data", dataJson);
		return paramJsonObj;
	}

	/*
	 * 初始化业务参数(实名认证)
	 *
	 * 姓名,身份证,手机号
	 *
	 * @return
	 */
	public JSONObject initMainServiceParamJsonObj_Realname() {
		/**
		 * 具体业务参数放在data中
		 */
		  JSONObject dataJson = new JSONObject();
          /*dataJson.put("name", SHA256Utils.String2SHA256("高峰明"));
	      dataJson.put("id_number",SHA256Utils.String2SHA256("152322196311063719"));
	      dataJson.put("mobile",SHA256Utils.String2SHA256("13104866236"));
	      dataJson.put("encrypt","SHA256");*/
		  dataJson.put("companyNum", "10004849");
	      dataJson.put("serviceStartTime","2022-04-15T17:30:37");
	      dataJson.put("serviceEndTime","2023-04-15T21:30:37");
	      dataJson.put("contractFileName","spring高级源码笔记msy.pdf");
	      dataJson.put("serviceType","3");
	      dataJson.put("base64Str",Base64Util.getPDFBinary(new File("C:\\Users\\msy\\Desktop\\spring高级源码笔记.pdf")));
		 /* List<String> list = new ArrayList<>();
			list.add("13752639577");
	        dataJson.put("msisdnmd5list",list);*/
		  return dataJson;
	}

	/**
	 * 实现http post请求 注意编码 UTF-8
	 *
	 * @param url
	 * @param paramObj
	 * @return
	 */
	public String postClient(String url, JSONObject paramObj) {
		String str = "";
		HttpClient client = null;
		PostMethod method = null;
		try {
			client = new HttpClient();
			method = new PostMethod(url);
			method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
			NameValuePair[] param = new NameValuePair[paramObj.size()];
			@SuppressWarnings("unchecked")
			Iterator<String> keys = paramObj.keys();
			int i = 0;
			while (keys.hasNext()) {
				String key = (String) keys.next();
				String value = paramObj.getString(key);
				param[i] = new NameValuePair(key, value);
				i++;
			}
			method.setRequestBody(param);
			client.executeMethod(method);
			str = convertStreamToString(method.getResponseBodyAsStream());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (method != null) {
				method.releaseConnection();
			}
		}
		return str;
	}

	/**
	 * 流转字符串
	 *
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder builder = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				builder.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return builder.toString();
	}

}
