开始对接

调用电话会议主动服务API接口, 需要全时为客户提供安全证书, 并需要开发者在代码中加载此证书。 以下是加载安全证书和发起HTTP请求的示例。

    private static boolean initialized = false;
    private static Logger logger = LogManager.getLogger(QsAPI.class);
    private static final String BASE_URL = "https://telopenapi.quanshi.com/tel-conference/deal/v1/" ;

    private static final String KEY_STORE_TYPE_JKS = "jks" ; 
    private static final String KEY_STORE_TYPE_P12 = "PKCS12" ; 
    private static final String SCHEME_HTTPS = "https" ; 
    private static final int HTTPS_PORT = 443;

    private static String KEY_STORE_CLIENT_PATH = ""; 
    private static String KEY_STORE_TRUST_PATH = ""; 
    private static final String KEY_STORE_PASSWORD = "111111";
    private static final String KEY_STORE_TRUST_PASSWORD = "111111";

    static SSLContext sslContext = null;
    static HttpClient httpClient = new DefaultHttpClient();

    /**
     * Initialize
     *
     */
    public static void init(String _p12Path, String _trustPath) {
        logger.debug("Init is beginning!");
        try {

            KEY_STORE_CLIENT_PATH = _p12Path;
            KEY_STORE_TRUST_PATH = _trustPath;

            System.setProperty("javax.net.ssl.keyStore", KEY_STORE_CLIENT_PATH);
             System.setProperty("javax.net.ssl.keyStorePassword", KEY_STORE_PASSWORD);
             System.setProperty("javax.net.ssl.keyStoreType", KEY_STORE_TYPE_P12);
             System.setProperty("javax.net.ssl.trustStore", KEY_STORE_TRUST_PATH);
             System.setProperty("javax.net.ssl.trustStorePassword", KEY_STORE_TRUST_PASSWORD);
             System.setProperty("javax.net.ssl.trustStoreType", KEY_STORE_TYPE_JKS);

             KeyStore kstore = KeyStore.getInstance(KEY_STORE_TYPE_P12);
             kstore.load(new FileInputStream(KEY_STORE_CLIENT_PATH), KEY_STORE_PASSWORD.toCharArray());
             KeyManagerFactory keyFactory = KeyManagerFactory.getInstance("sunx509");
             keyFactory.init(kstore, KEY_STORE_PASSWORD.toCharArray());
             KeyStore tstore = KeyStore.getInstance(KEY_STORE_TYPE_JKS);
             tstore.load(new FileInputStream(KEY_STORE_TRUST_PATH), KEY_STORE_TRUST_PASSWORD.toCharArray());

             TrustManager[] tm;
             TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
             tmf.init(tstore);
             tm = tmf.getTrustManagers();

             sslContext = SSLContext.getInstance("SSL");
             sslContext.init(keyFactory.getKeyManagers(), tm, null);

            initialized = true;
            logger.debug("Init is success!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("Init error:please check cert path is set correctly");
        }
    }

初始化后, 发起HTTP请求

public static String httpPost(String url, String jsonParam) {
        try {
            httpClient = new DefaultHttpClient();
            SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext);
            Scheme sch = new Scheme(SCHEME_HTTPS, HTTPS_PORT, socketFactory);
            httpClient.getConnectionManager().getSchemeRegistry().register(sch);
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            httpPost.addHeader("charset", HTTP.UTF_8);
            httpPost.setEntity(new StringEntity(jsonParam.toString(), HTTP.UTF_8));

            logger.info("executing request " + httpPost.getRequestLine());
            logger.info("executing params " + jsonParam.toString());
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null ) {

                String result = EntityUtils.toString(entity,"utf-8");
                logger.info("Response content is : " + result);

                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return "";
    }