標籤:餘額 channel .exe exe methods net pre .com private
/* * 建立日期 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資料是:\n"+jsonObject.toString()); System.out.println("從銀企直聯平台查詢帳號列表,接收到的響應是:"); //調用商務邏輯方法,取得返回的json並列印 String resultString=new BankCommandService().getAccountList(jsonObject.toString()); System.out.println(resultString); }}
使用HttpClient實現對第三方伺服器的請求並接受返回資料