使用HttpClient實現對第三方服務器的請求並接受返回數據


/*
 * 創建日期 2017-4-7
 *
 * 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數據是:\n"+jsonObject.toString());
            System.out.println("從銀企直聯平台查詢賬號列表,接收到的響應是:");
            //調用業務邏輯方法,取得返回的json並打印
           String resultString=new BankCommandService().getAccountList(jsonObject.toString());
           System.out.println(resultString);       
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM