118 lines
4.7 KiB
Plaintext
118 lines
4.7 KiB
Plaintext
package com.nationalelectric.greenH5.utils;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import org.apache.http.HttpEntity;
|
||
import org.apache.http.HttpResponse;
|
||
import org.apache.http.NameValuePair;
|
||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||
import org.apache.http.client.methods.HttpPost;
|
||
import org.apache.http.impl.client.CloseableHttpClient;
|
||
import org.apache.http.impl.client.HttpClients;
|
||
import org.apache.http.message.BasicNameValuePair;
|
||
import org.apache.http.util.EntityUtils;
|
||
|
||
import java.net.URLDecoder;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* @Author:Lin
|
||
* @create:2020/5/29 14:20
|
||
*/
|
||
|
||
|
||
public class GetTokenUtil {
|
||
public static void main(String[] args) throws Exception {
|
||
|
||
// methodPath从能力开放平台获取
|
||
String url = "http://124.126.19.5:19118/protocolTrans/openapi/ShopService_getUserToken";
|
||
JSONObject model = new JSONObject();
|
||
JSONObject body = new JSONObject();
|
||
// 在调用接口前需要先进行平台认证,获取token
|
||
// 将能力开放平台需要的参数封装入body
|
||
body.put("appId", "40282a077337e97601734b09cca80180"); // appId必填,不加密
|
||
body.put("authToken", encrypt("myAuthToken")); // authToken必填,需加密
|
||
model.put("phone", "13888888888");
|
||
model.put("card_no", "123123");
|
||
model.put("system_code", "300028");
|
||
model.put("name", "张三");
|
||
model.put("department", "");
|
||
System.out.println(model.toJSONString());
|
||
body.put("map", encrypt(model.toJSONString()));
|
||
|
||
// // 将业务数据封装进body中,参数为key-value形式,key不加密,value根据接口文档的要求进行加密
|
||
// // 如果参数是java本身的对象就直接封装入body
|
||
// body.put("stakeNo", encrypt("0")); // int型
|
||
// body.put("orderDate", encrypt("2018-01-01"));// date型数据
|
||
// // 如果参数是object,先封装进一个json对象中,再把这个json对象封装进body
|
||
// model.put("password", "ddqc_123");
|
||
// model.put("userName", "isv_mrst");
|
||
// int[] seq = { 1, 2, 3 };
|
||
// model.put("startChargeSeq", seq); // list[int]型
|
||
// model.put("businessLabel", 5);
|
||
// body.put("model", encrypt(model.toJSONString()));// 加密
|
||
// 将所有参数封装入args
|
||
Map<String, Object> map = new HashMap<String, Object>();
|
||
map.put("args", body); // 这里必须是args
|
||
// 调用http,获取返回值
|
||
String jsonStr = mapPost(url, map, "utf-8");
|
||
// 解析返回值,返回值均在"data"中
|
||
JSONObject jsonObj = JSONObject.parseObject(jsonStr);
|
||
jsonObj.put("data", URLDecoder.decode(dencrypt(jsonObj.getString("data"))));// 为防止中文乱码
|
||
|
||
// status和info是开放平台加入的参数,供ISV判断接口是否调用成功。
|
||
if (jsonObj.getString("status").equals("0")) {
|
||
// 其他参数根据接口文档进行解析
|
||
JSONObject para1 = jsonObj.getJSONObject("para1");
|
||
System.out.println(para1.toJSONString());
|
||
}
|
||
}
|
||
|
||
public static String encrypt(String data) throws Exception {
|
||
String pass_key = "63c65fce72684253";
|
||
String iv_str = "99dbcce28d6d443e";
|
||
return AESUtil.Encrypt(data, pass_key, iv_str);
|
||
}
|
||
|
||
public static String dencrypt(String data) throws Exception {
|
||
String pass_key = "63c65fce72684253";
|
||
String iv_str = "99dbcce28d6d443e";
|
||
return AESUtil.Decrypt(data, pass_key, iv_str);
|
||
}
|
||
|
||
public static String mapPost(String url, Map<String, Object> map, String encoding) {
|
||
CloseableHttpClient httpClient = null;
|
||
HttpPost httpPost = null;
|
||
String result = null;
|
||
try {
|
||
httpClient = HttpClients.createDefault();
|
||
httpPost = new HttpPost(url);
|
||
|
||
List<NameValuePair> list = new ArrayList<NameValuePair>();
|
||
Iterator iterator = map.entrySet().iterator();
|
||
while (iterator.hasNext()) {
|
||
Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
|
||
list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
|
||
}
|
||
if (list.size() > 0) {
|
||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encoding);
|
||
httpPost.setEntity(entity);
|
||
}
|
||
HttpResponse response = httpClient.execute(httpPost);
|
||
if (response != null) {
|
||
HttpEntity resEntity = response.getEntity();
|
||
if (resEntity != null) {
|
||
result = EntityUtils.toString(resEntity, encoding);
|
||
}
|
||
}
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|