工作记录(五) 使用httpClient 调起https url接口


******遇到的问题*******
1、ssl证书信任,解决方式信任所有证书
2、生成请求体调用httpPost.setEntity()时输出的参数格式有误,有多种Entity可以选择(常用 StringEntity、UrlEncodedFormEntity、FileEntity等),根据参数格式选择合理的

一、LookAlikeHttpService 
/**
 * 请求lookAlike HTTP调起工具类
 *
 * @author zhaoxinyu
 * @create 2018/04/26
 */
@Service("lookAlikeHttpService")
public class LookAlikeHttpService extends DefaultHttpClient {
    private final String charset = "utf-8";
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static SSLConnectionSocketFactory sslsf = null;
    private static PoolingHttpClientConnectionManager cm = null;
    private static SSLContextBuilder builder = null;

    static {
        try {
            builder = new SSLContextBuilder();
            // 全部信任 不做身份鉴定
            builder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            });
            sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register(HTTP, new PlainConnectionSocketFactory())
                    .register(HTTPS, sslsf)
                    .build();
            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);//max connection
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * httpClient post请求
     * @param url 请求url
     * @param param 请求参数 form提交适用
     * @return 可能为空 需要处理
     * @throws Exception
     *
     */
    public static String sendHttpsPost(String  url, String param) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpPost httpPost = new HttpPost(url);
            /**
             * 设置请求头
             */
            httpPost.addHeader("Content-Type", "application/json");
            httpPost.addHeader("userName","aml4rest");
            httpPost.addHeader("passWord","3er4#ER$3er4#ER$12");
            /**
             *  设置请求参数
             */
            StringEntity stringEntity = new StringEntity(param, "utf-8");
            httpPost.setEntity(stringEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity);
            } else {
                result = readHttpResponse(httpResponse);
            }
            
        } catch (Exception e) {
            System.out.println("send lookalike http post request failed, HttpException"+e);
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        System.out.println("lookAlikeHttpService doPost(..); result=" + result + ";");
        return result;
    }

    public String readFromInputStream(InputStream in, String charset) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(in,charset), 2048);
        LineIterator li = new LineIterator(br);
        while (li.hasNext())
        {
            if (sb.length() > 5000)
            {
                continue;
            }
            sb.append(li.next());
        }
        return sb.toString();
    }
    
    public static CloseableHttpClient getHttpClient() throws Exception {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setConnectionManager(cm)
                .setConnectionManagerShared(true)
                .build();
        return httpClient;
    }
    
    public static String readHttpResponse(HttpResponse httpResponse)
            throws ParseException, IOException {
        StringBuilder builder = new StringBuilder();
        // 获取响应消息实体
        HttpEntity entity = httpResponse.getEntity();
        // 响应状态
        builder.append("status:" + httpResponse.getStatusLine());
        builder.append("headers:");
        HeaderIterator iterator = httpResponse.headerIterator();
        while (iterator.hasNext()) {
            builder.append("\t" + iterator.next());
        }
        // 判断响应实体是否为空
        if (entity != null) {
            String responseString = EntityUtils.toString(entity);
            builder.append("response length:" + responseString.length());
            builder.append("response content:" + responseString.replace("\r\n", ""));
        }
        return builder.toString();
    }
}

二、组装参数方法
/**
 * 根据任务信息调起任务
 */
public JSONObject invokeAlgorithm(TaskInfo taskInfo) throws Exception {
    String path = propertyUtil.getProperty("invokePath");
    
    String dataPath = propertyUtil.getProperty("hadoopBasePath")+ File.separator +"hc_data_"+String.valueOf(taskInfo.gettId())+".txt";
    String schemaPath = propertyUtil.getProperty("hadoopBasePath")+ File.separator +"hc_schema_"+String.valueOf(taskInfo.gettId())+".txt";
    String labelFeatureId = String.valueOf((customerInfoService.getCustomerListBytId(String.valueOf(taskInfo.gettId())).size()+1));

    JSONObject params = new JSONObject();
    params.put("dataPath", dataPath);
    params.put("schemaPath", schemaPath);
    params.put("delimiter", "|");
    params.put("primaryKeyId", "0");
    params.put("businessLabel", "lookalike");
    params.put("businessId", String.valueOf(taskInfo.getsId()));
    params.put("labelFeatureId", labelFeatureId);

    JSONObject parameter = new JSONObject();
    parameter.put("parameter",params);
    
    JSONObject result = JSONObject.fromObject(lookAlikeHttpService.sendHttpsPost(path,parameter.toString()));
    return result;
}


 

猜你喜欢

转载自my.oschina.net/dreambreeze/blog/1810449