天天看点

java http服务端获取返回的数据_使用HttpClient实现对第三方服务器的请求并接受返回数据...

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;

public classBankCommandService {private static String rootURL="";//银企直联平台服务器地址

private static Properties commandProperties=newProperties();//读取银企直联平台配置文件的属性

static{try{

commandProperties.load(BankCommandService.class.getResourceAsStream("BankCommand.properties"));

rootURL=commandProperties.getProperty("rootURL");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

publicString sendCommand(String commandURL,String sendJson){

String resultJson="银企直连平台返回异常";try{//新建HttpClient对象,用于访问银企直联平台;

HttpClient httpClient = newHttpClient();

PostMethod postMethod= newPostMethod(commandURL);//让post请求携带json数据

RequestEntity requestEntity = new StringRequestEntity(sendJson,"application/json", "UTF-8");

postMethod.setRequestEntity(requestEntity);//发送post请求

httpClient.executeMethod(postMethod);//得到从银企直联响应的json数据

resultJson = newString(postMethod.getResponseBody());

}catch(Exception e) {

e.printStackTrace();

}returnresultJson;

}

publicString directPay(String sendJson){

String commandURL=rootURL+commandProperties.getProperty("directPayCommand");returnsendCommand(commandURL,sendJson);

}

publicString getBalanceBatch(String sendJson){

String commandURL=rootURL+commandProperties.getProperty("getBalanceBatchCommand");returnsendCommand(commandURL,sendJson);

}

publicString getAccountList(String sendJson){

String commandURL=rootURL+commandProperties.getProperty("getAccountListCommand");returnsendCommand(commandURL,sendJson);

}

public static voidmain(String[] args) {

JSONObject jsonObject= newJSONObject();

JSONObject headvalue=newJSONObject();

JSONObject bodyvalue=newJSONObject();

JSONObject infovalue=newJSONObject();

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=newBankCommandService().getAccountList(jsonObject.toString());

System.out.println(resultString);

}

}