zoukankan      html  css  js  c++  java
  • 使用HttpClient实现对第三方服务器的请求并接受返回数据

    /*
     * 创建日期 2017-5-24
     *
     * TODO 要更改此生成的文件的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    package com.enfo.intrust.command;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.*;
    import net.sf.json.JSONObject;
    import java.util.Properties;
    import java.io.IOException;
    
    /**
     * @author tapt
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    
    public class BankCommandService {
        private static String rootURL="";//银企直联平台服务器地址
        private static Properties commandProperties=new Properties();
        //读取银企直联平台配置文件的属性
        static{
            try {
                commandProperties.load(BankCommandService.class.getResourceAsStream("BankCommand.properties"));
                rootURL=commandProperties.getProperty("rootURL");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        /**
         * @TODO 通用方法,传入一个json,连接银企直联平台,返回平台响应的json
         * */
        public String sendCommand(String commandURL,String sendJson){
            String resultJson="银企直连平台返回异常";
            try {      
                // 新建HttpClient对象,用于访问银企直联平台;
                HttpClient httpClient = new HttpClient();
                PostMethod postMethod = new PostMethod(commandURL);
                // 让post请求携带json数据
                RequestEntity requestEntity = new StringRequestEntity(sendJson,"application/json", "UTF-8");
                postMethod.setRequestEntity(requestEntity);
                // 发送post请求
                httpClient.executeMethod(postMethod);
                // 得到从银企直联响应的json数据
               resultJson = new String(postMethod.getResponseBody());
            } catch (Exception e) {
                e.printStackTrace();
            }                
            return resultJson;
        }
        
        /**
         * @TODO 直接划款
         * */
        public String directPay(String sendJson){
            String commandURL=rootURL+commandProperties.getProperty("directPayCommand");
            return sendCommand(commandURL,sendJson);
            }
        
        /**
         * @TODO 批量查询余额
         * */
        public String getBalanceBatch(String sendJson){
            String commandURL=rootURL+commandProperties.getProperty("getBalanceBatchCommand");
            return sendCommand(commandURL,sendJson);
            }
        
        /**
         * @TODO 查询账户列表
         * */    
        public String getAccountList(String sendJson){
            String commandURL=rootURL+commandProperties.getProperty("getAccountListCommand");
            return sendCommand(commandURL,sendJson);
            }
        
        /**
         * @TODO 用于测试银企直联返回数据的方法-查询所有账户列表
         * */
        public static void main(String[] args) {
                JSONObject jsonObject = new JSONObject();
                JSONObject headvalue=new JSONObject();
                JSONObject bodyvalue=new JSONObject();
                JSONObject infovalue=new JSONObject();
                headvalue.put("request_no", "001201612221707000002");
                headvalue.put("device_type", "1");
                headvalue.put("cust_id", "1122345452");
                headvalue.put("router", "1");
                headvalue.put("channel", "01");
                headvalue.put("app_id", "0001");
                headvalue.put("charset", "UTF-8");
                headvalue.put("version", "1.0.0.1");
                headvalue.put("sign", "MScRd7GM52W41VpRGxn7BtNWsSLM/RZPzbIGjxQFiChQcN8CXTjFU9MVtDP7NXxgZZddVc+NOc+P91anV9fQ1TjtdYZJr5hg1xPP/CAokB5LlxANnc+UfBcGQWGRGjXa/wijRPvdu7hiHEKW4dNt6giQgQMlcH/1eobXY5Z4pmU=");
                headvalue.put("language", "CN");
                
                jsonObject.put("head", headvalue);
                
                infovalue.put("buscod", "n03010");
                infovalue.put("busmod", "00001");
                
                bodyvalue.put("info", infovalue);
                
                jsonObject.put("body", bodyvalue);
              //创建查询账户列表的发送json
                System.out.println("要传入到银企直联的json数据是:
    "+jsonObject.toString());
                System.out.println("从银企直联平台查询账号列表,接收到的响应是:");
                //调用业务逻辑方法,取得返回的json并打印
               String resultString=new BankCommandService().getAccountList(jsonObject.toString());
               System.out.println(resultString);       
        }
    }
  • 相关阅读:
    关于求 p_i != i and p_i != i+1 的方案数的思考过程
    poj 3041 Asteroids 二分图最小覆盖点
    poj 1325 Machine Schedule 最小顶点覆盖
    poj 1011 Sticks 减枝搜索
    poj 1469 COURSES 最大匹配
    zoj 1516 Uncle Tom's Inherited Land 最大独立边集合(最大匹配)
    Path Cover (路径覆盖)
    hdu 3530 SubSequence TwoPoint单调队列维护最值
    zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
    poj 1466 Girls and Boys 二分图最大独立子集
  • 原文地址:https://www.cnblogs.com/xiaowenzi/p/6897716.html
Copyright © 2011-2022 走看看