IPSDemo

作者: zjlearn | 来源:发表于2016-07-28 21:28 被阅读222次

    IPS系统接口说明

    因为IPS系统接口分为以下两种

    • POST页面跳转,
    • POST后台接口

    POST页面跳转

    POST页面跳转的方式是将表单中的参数通过页面跳转的方式 提交到IPS系统中去。
    下面是你给我的真实系统的代码:

    <html>
     <head>
      <script type="text/javascript">window.onload=function(){document.getElementById('submitForm').submit();}</script>
     </head>
     <body>
      <form action="https://UFunds.ips.com.cn/p2p-deposit/gateway.htm" id="submitForm" method="post">
       <input type="hidden" name="operationType" value="user.register" />
       <input type="hidden" name="merchantID" value="1801310028" />
       <input type="hidden" name="sign" value="ad7ad8a00b6d93a9d096f38c78be7df6" />
       <input type="hidden" name="request" value="ISY0xYCcDuhwsweGwOyeUN0p+x4PdKMgX7YJjPkxgOIcAMaJ/UMT5MtFP3P7e/CRfiRfzGN8sbfEWsB8QIAzA1k7D1dFEuezRISBja8OBsCmj5DZNvbIZfUDnoOzA0OAMg7TnIwfpEnGOOBHH9zjORJj5qHGj+OIhkoQ/IY0XwAHYqRtCTfJb5HQzKi+TJDeQkC7GQAj+gCkQoTMrWSaykpOoiI1fANxlFPKq9maj/E6MXqwzVUQlc3Lx5kGJRt81aYTTd5D4Q9Luw/WP2hsuL/7BH74QOs9HsrDP7vDCX3MIPmI9DbmdNEYdj6Q0fOnQJI4SxgTTlTor8KxAWd1s4+axysbI/fdoOWs2u0u6lW1Ye+1Qiyi5w==" />
      </form> 
     </body>
    </html>
    

    这个是当在系统中点击开户时,会跳转到的页面的源代码。
    这个源代码的含义是:当页面载入时,会自动提交相应的表单,到"https://UFunds.ips.com.cn/p2p-deposit/gateway.htm
    表单的属性包含了

    • operationType
    • merchantID
    • sign
    • request
      正好为IPS系统接口文档中的参数。

    我们可以做个试验,将上面的代码,去掉javascript代码(主要功能在于自动提交)然后显示的加个提交的按钮。然后保存为html页面。打开页面, 点击提交之后,就会转到开户的页面。
    源代码如下:

    <html>
    <head>
     </head>
     <body>
      <form action="https://UFunds.ips.com.cn/p2p-deposit/gateway.htm" id="submitForm" method="post">
       <input  name="operationType" value="user.register" />
       <input  name="merchantID" value="1801310028" />
       <input  name="sign" value="ad7ad8a00b6d93a9d096f38c78be7df6" />
       <input  name="request" value="ISY0xYCcDuhwsweGwOyeUN0p+x4PdKMgX7YJjPkxgOIcAMaJ/UMT5MtFP3P7e/CRfiRfzGN8sbfEWsB8QIAzA1k7D1dFEuezRISBja8OBsCmj5DZNvbIZfUDnoOzA0OAMg7TnIwfpEnGOOBHH9zjORJj5qHGj+OIhkoQ/IY0XwAHYqRtCTfJb5HQzKi+TJDeQkC7GQAj+gCkQoTMrWSaykpOoiI1fANxlFPKq9maj/E6MXqwzVUQlc3Lx5kGJRt81aYTTd5D4Q9Luw/WP2hsuL/7BH74QOs9HsrDP7vDCX3MIPmI9DbmdNEYdj6Q0fOnQJI4SxgTTlTor8KxAWd1s4+axysbI/fdoOWs2u0u6lW1Ye+1Qiyi5w==" />
        <input type="submit" value="Submit" />
      </form> 
     </body>
    </html>
    

    上面就是POST页面跳转方式工作方式。

    ** 现在不清楚的是: ** 提交之后,返回的是一个页面,而文档中给出的返回结果却是json格式的字符串。

    POST后台接口

    POST后台接口,也是一个HTTP web接口,需要在后台通过http协议来进行通信传输。
    HTTP可以理解为一种通信双方的协议约定。这里的POST指的是POST方法。

    POST后台接口,也需要IPS系统接口中指明的,来准备数据。然后就可以通信的结果中得到接口中指明的response.

    JSON

    JSON 是一种数据格式,用key:value方式组建而成。
    详细的介绍可以参考:
    http://www.w3school.com.cn/json/

    JSON对象的构建

    (采用alibaba的fastJson的包。)
    JSON对象 与JSON字符串的区别: JSON对象是一个对象,JSON字符串是一个字符串。JSON对象有方法,可以根据key得到相应的value.而JSON字符串不行。

    JSONObject requestJson=new JSONObject();
    

    JSONObject 就是JSON对象的类型。类是有方法的。
    阿里巴巴的fastJson工具包提供了以下几个关键函数用户处理JSON对象。

        //生成JSONObject,内容为空
        public JSONObject() {
            this(16, false);
        }
          //通过map集合 生成JSONObject,
        public JSONObject(Map<String, Object> map) {
            this.map = map;
        }
        //获取key对应的value
        public Object get(Object key) {
            return this.map.get(key);
        }
        //将key,value 放到JSONObject中去
        public Object put(String key, Object value) {
            return this.map.put(key, value);
        }
        //将JSONObject转化为字符串,其调用的是toJSONString
        public String toString() {
            return this.toJSONString();
        }
        //将JSON字符串转化为JSONObject,方法为static
         public static JSONObject parseObject(String text) {
            Object obj = parse(text);
            return obj instanceof JSONObject?(JSONObject)obj:(JSONObject)toJSON(obj);
        }
        //将object对象转化为JSON字符串
         public static String toJSONString(Object object) 
    

    所以通过以下几种方式构建JSON字符串。

    对象转化

    例如,假设有个User类,然后其具有属性,那么从User构建的JSON字符串就是

    class User{
      public String name;
      public String age;
    public User(String name , String age){
          this.name=name;
          this.age=age;
      }
    }
    
    User u=new User("zj","23");
    String r=JSON.toJSONString(user)
    

    最后得到的字符串就类似于

    {"name":"zj",
    "age":"23"
    }
    

    JSONObject

    直接构造JSONObject,然后填入key,value,然后转化为String .
    例子如下:

    import com.alibaba.fastjson.JSONObject;
    
    JSONObject jsonObject = new JSONObject();  
            jsonObject.put("f1", "xxx");
            jsonObject.put("f2", "xxx");
            String json = jsonObject.toJSONString()
    

    后台POST接口的实现

    我们以查询为例。

    实现请求报文格式中的request参数

    JSONObject requestJson=new JSONObject();
            requestJson.put(    "ipsAcctNo", "");
            requestJson.put("queryType", "02");
            requestJson.put("merBillNo", "1023654");
            String requestString=requestJson.toString();
    

    构造完request参数,我们可以发送http报文到IPS服务器了

    IpsResponse respon= RequestUtil.request("query.commonQuery", ModelParam.merchantID, requestString);
    

    上面的 RequestUtil.request(...)是我们自己实现的一个函数,其目的是将参数封装到报文中,发送到服务器,得到结果。

    RequestUtil.request

    源代码以及注释如下:
    为了简便程序的编写,故借助一些http包来实现http通信。
    包为:org.apache.httpcomponents

    public class RequestUtil {
        public static IpsResponse request(String operationType,  String merchantID, String request){
             CloseableHttpClient httpclient =HttpClients.createDefault(); 
             
                //每个系统的换行符 不一样
                 String lineSeparator = System.getProperty("line.separator", "\n");
    
            //对request进行加密
                String encrypedRequest=IpsCrypto.triDesEncrypt(request, ModelParam.encryptKey, ModelParam.iv).replaceAll(lineSeparator, "");
    
                //MD5 for sign
                String  sign=operationType+merchantID+encrypedRequest+ModelParam.mdstr;   
                String signMD5=IpsCrypto.md5Sign(sign).toLowerCase();
    
            //设置http报文的参数, operationType, merchantID,sign, request
                List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                formparams.add(new BasicNameValuePair("operationType", operationType));
                formparams.add(new BasicNameValuePair("merchantID", ModelParam.merchantID));
                formparams.add(new BasicNameValuePair("sign", signMD5));
                formparams.add(new BasicNameValuePair("request", encrypedRequest));
                UrlEncodedFormEntity urlentity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
                //新建一个HttpPost代表发送的是http协议,POST方法。 ModelParam.TestServerAdderss是常量, 保存在ModelParam类中
                //这里测试系统,所以为     public final static String TestServerAdderss="http://180.168.26.114:20010/p2p-deposit/gateway.htm";
                HttpPost httppost = new HttpPost(ModelParam.TestServerAdderss);
                //设置构造的表单参数
                httppost.setEntity(urlentity);
                
                try {
                    // 执行httppost, 也就是将请求报文发送出去,得到结果。
                    CloseableHttpResponse response = httpclient.execute(httppost);
    
                    HttpEntity entity = response.getEntity();
                    long len= entity.getContentLength();
                    //获得返回结果的内容,将其读入到text字符串中
                    Reader reader= new BufferedReader( new InputStreamReader(entity.getContent()));
                    char [] data= new char[1024];
                    int readLen=0;
                    StringBuilder sb= new StringBuilder();
                     if( (readLen=reader.read(data))!= -1){
                        sb.append(data,0,readLen);
                    }
                    String text=sb.toString();
                    //现在从IPS系统中获取到了所有的数据, 存储在了text字符串中, 字符串可能包含两种类型。html或者json
                    if(entity.getContentType().getValue().contains("html")){
                        System.out.println(text);
                        return new IpsResponse(text);
                    }else { //json格式的字符串
                        //从字符串转到JSONObject,这样我们就可以根据key得到value.
                        JSONObject result = JSONObject.parseObject(text);
                        System.out.println("the  result code is " + (String) result.get("resultCode"));
                        //我们构造一个IpsResponse对象代表返回的结果
                        return new IpsResponse((String) result.get("resultCode"), (String) result.get("resultMsg"), (String) result.get("sign"), (String) result.get("response"));
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("get some exception!\n");
                }
                System.out.println("new Response!");
                return new IpsResponse("exception!\n");
        }
    }
    
    

    然后我们看下
    IpsResponse仅仅是返回接口的封装而已。

    public class IpsResponse {
        public String resultCode;
        public  String resultMsg;
        public String sign;
        public String response;
        public String message;  //当返回的是html页面时, 保存html页面
        public  Boolean isJson;  //判断是否是json格式的字符串
        public IpsResponse(String resultCode, String resultMsg, String sign, String response){
            this.resultCode=resultCode;
            this.resultMsg=resultMsg;
            this.sign=sign;
            this.response=IpsCrypto.triDesDecrypt(response,ModelParam.encryptKey, ModelParam.iv);
            //System.out.println(response);
            this.isJson=true;
        }
        //this constructor handle exception contidional
        public IpsResponse(String message){
            this.message=message;
            this.isJson=false;
        }
        @Override
        public String toString() {
            if(isJson)
                return "IpsResponse is: "+"\n"+
                        "resultCode: "+resultCode+"\n"+
                        "resultMsg: "+resultMsg +"\n"+
                        "response: "+response;
            else
                return  message;
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:IPSDemo

          本文链接:https://www.haomeiwen.com/subject/ymgajttx.html