调用第三方接口

public class InsuranceUtil {



    public static JSONObject getHttpResponseJson(String url,Map param,String XAPPID,String XAPPKEY){
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        JSONObject jsonObject = null;

        String jsonString = com.alibaba.fastjson.JSONObject.toJSONString(param);

        try {
            //请求发起客户端
            httpclient = HttpClients.createDefault();
            //参数集合
//            if (null == param) {
//                param = new HashMap();
//            }
//            List pairList = new ArrayList(param.size());
//            for (Map.Entry entry : param.entrySet()) {
//                NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
//                pairList.add(pair);
//            }
//            表单方式提交
//            HttpEntity paramEntity = new UrlEncodedFormEntity(pairList,"UTF-8");
//            post.setEntity(paramEntity);

            //通过post方式访问
            HttpPost post = new HttpPost(url);
            post.setHeader("Content-Type", "application/json;charset=UTF-8");
//            post.addHeader("X-APP-ID",XAPPID);
//            post.addHeader("X-APP-KEY",XAPPKEY);

            StringEntity stringEntity = new StringEntity(jsonString, "UTF-8");
            stringEntity.setContentType("TEXT/JSON");
            post.setEntity(stringEntity);

            response = httpclient.execute(post);
            HttpEntity valueEntity = response.getEntity();
            String content = EntityUtils.toString(valueEntity);
             jsonObject = JSONObject.parseObject(content);
            return jsonObject;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(httpclient != null){
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return jsonObject;
    }


}

你可能感兴趣的:(调用第三方接口)