武电实业卡密接口对接经验分享

以往卡密都是电信直接提供txt文件我们这边再导入数据库中,现在电信要求厂家获取卡密必须通过接口调用,此次对接主要用到三个方面知识:

1.通过http Post请求方式将JSON数据放入body中传入,http数据交互post方式常用DefaultHttpClient(需要导入httpclient-4.1.jar,httpcode-4.1.jar,commons-logging-1.1.1.jar

 1 public static String post(String url, String content){
 2         String charsetName = "utf-8";
 3         DefaultHttpClient httpclient = null;
 4         HttpPost post = null;
 5         try {
 6             httpclient = new DefaultHttpClient();
 7             post = new HttpPost(url);
 8             post.setHeader("Content-Type", "application/json;charset=" + charsetName);
 9             post.setEntity(new StringEntity(content, charsetName));
10             HttpResponse response = httpclient.execute(post);
11             HttpEntity entity = response.getEntity();
12             String rsp = EntityUtils.toString(entity, charsetName);
13             return rsp;
14         } catch (Exception e) {
15             throw new RuntimeException(e);
16         } finally {
17             try {
18                 httpclient.getConnectionManager().shutdown();
19             } catch (Exception ignore) {}
20         }
21     }

2.需要将传入的code以AES加密:

武电实业提供文档如下:

主要对传入json串中的code值进行加密(早期的对称加密方式为DES,DES被破解,又出来3DES加密算法基于DES基础之上,所以也很快被破解了,目前最为流行的为AES),博客园里有位博友的文章写的不错,大家有兴趣的可以看看: http://www.cnblogs.com/block2016/p/5596676.html

加密方法如下:

 1 public static String Encrypt(String sSrc, String sKey) throws Exception {
 2         if (sKey == null) {
 3             System.out.print("Key为空null");
 4             return null;
 5         }
 6         // 判断Key是否为16(此处为AES 128位)
 7         if (sKey.length() != 16) {
 8             System.out.print("Key长度不是16");
 9             return null;
10         }
11         byte[] raw = sKey.getBytes();
12         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
13         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
14         IvParameterSpec iv = new IvParameterSpec("1Zja83t262AXxn13".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
15         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
16         byte[] encrypted = cipher.doFinal(sSrc.getBytes());
17 
18         return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
19     }

 3.返回值的获取,通过org.json.JSONObject(需要导入json.jar)对象解析json数据(android处理json数据经常用到,我们iptv业务处理json数据是采用jackson,需要导入jackson-all-1.9.5.jar);

以下我将两种方式简单的写了出来:

 1     /**
 2      * {"dataInfo":{"cardNo":"itv200085","cardPwd":"ps2000085"},"message":"","status":"0"}
 3      * 对上面的json进行解析,获取所需要的值cardNo
 4      */
 5     /**
 6      * JSONObject方式解析json串
 7      */
 8     public static String JSONObectToString(String json){
 9         try {
10             JSONObject jsonObject = new JSONObject(json);
11             JSONObject dataInfo = jsonObject.getJSONObject("dataInfo");
12             String cardNo = dataInfo.getString("cardNo");
13             return cardNo;
14         } catch (JSONException e) {
15             e.printStackTrace();
16         }
17         return null;
18     }
19     /**
20      * Jackson方式解析json串
21      */
22     public static String JacksonToString(String json){
23         ObjectMapper mapper = new ObjectMapper();
24         try {
25             Map object = mapper.readValue(json, Map.class);
26             Map map = new HashMap<>();
27             map = (Map) object.get("dataInfo");
28             String cardNo = (String) map.get("cardNo");
29             return cardNo;
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33         return null;
34     }

若以上有哪些语句不对的麻烦大家及时指出,相互学习相互进步,谢谢阅读。

posted @ 2017-09-25 12:19  蟹丸  阅读(1104)  评论(1编辑  收藏  举报