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

本文展示了如何使用Java的HttpClient库实现与第三方服务器的HTTP通信,包括POST请求的设置、发送JSON数据和接收返回的JSON响应。示例中详细地说明了如何构建请求并处理银企直联平台的响应。
摘要由CSDN通过智能技术生成

/** 创建日期 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 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();

}

}/**

* @TODO 通用方法,传入一个json,连接银企直联平台,返回平台响应的json

**/

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;

}/**

* @TODO 直接划款

**/

publicString directPay(String sendJson){

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

}/**

* @TODO 批量查询余额

**/

publicString getBalanceBatch(String sendJson){

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

}/**

* @TODO 查询账户列表

**/

publicString getAccountList(String sendJson){

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

}/**

* @TODO 用于测试银企直联返回数据的方法-查询所有账户列表

**/

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值