/*
* 创建日期 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", "");
headvalue.put("device_type", "");
headvalue.put("cust_id", "");
headvalue.put("router", "");
headvalue.put("channel", "");
headvalue.put("app_id", "");
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", ""); 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实现对第三方服务器的请求并接受返回数据的更多相关文章

  1. java 接口中模拟浏览器 请求webservice 接受返回数据

    使用HttpClient 所需jar:commons-codec-1.9.jar,commons-httpclient-3.1.jar try { HttpClient client = new Ht ...

  2. 使用new Image()可以针对单单请求,不要返回数据的情况

    使用new Image()可以针对单单请求,不要返回数据的情况,比如我这里写了一个Demo,请求百度的Logo一个示例: <html> <head> </head> ...

  3. httpclient post请求带参数返回数据乱码问题解决

    客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...

  4. Fiddler -工具使用介绍(附:拦截请求并修改返回数据)(转)

    一.Fiddler 介绍 Fiddler 是一个使用 C# 编写的 http 抓包工具.它使用灵活,功能强大,支持众多的 http 调试任务,是 web.移动应用的开发调试利器. 1,功能特点 同 H ...

  5. axios 跨域请求允许带cookie,则服务器Access-Control-Allow-Origin应设置为具体域名,否则请求无法获得返回数据

    1.通过允许跨域访问实现了跨域请求,但为了使每个请求带上session信息,我设置了withCredentials ,即: axios.defaults.withCredentials = true ...

  6. okhttp 通过网关请求服务端返回数据

    1.启动类代码 package com.tycoon.service; import org.springframework.boot.SpringApplication; import org.sp ...

  7. PHP通过curl向其它服务器发请求并返回数据

    在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...

  8. HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

    1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /*  * 演示通过HttpClie ...

  9. android中用get和post方式向服务器提交请求

    通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...

随机推荐

  1. vue.js与react.js相比较的优势

    vue.js的简介 vue.js是一个javascript mvvm库,它是以数据驱动和组件化的思想构建的.我们平时多用js去操作dom,vue.js则是使用了数据绑定驱动来操作dom的,也就是说创建 ...

  2. The Jaisalmer Desert Festival 2017/2/9

    原文 India's Golden City celebrates its culture with costumes(服装),crafts(工艺品) and camels One of the fe ...

  3. 验证 .NET 4.6 的 SIMD 硬件加速支持的重要性

    SIMD 的意思是 Single Instruction Multiple Data.顾名思义,一个指令可以处理多个数据. .NET Framework 4.6 推出的 Nuget 程序包 Syste ...

  4. Android(java)学习笔记151:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  5. kafka 安装以及测试

    1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper  ...

  6. 【page-monitor 前端自动化 下篇】 实践应用

    转载文章:来源(靠谱崔小拽) 通过page-diff的初步调研和源码分析,确定page-diff在前端自动化测试和监控方面做一些事情.本篇主要介绍下,page-diff在具体的实践中的一些应用 核心d ...

  7. CAP 可用性理解

    从容灾角度看可用性. 多机同时返回. 主通过 heart-beat 脑裂. 用 paxos. 性能远距离. 对整体压力较大. 从用户体验的角度看单数据可用性: 不考虑城市灾备的情况发生.只有单机房的 ...

  8. ios设备屏幕尺寸与分辨率

    iOS 设备的屏幕尺寸.分辨率及其屏幕边长比例详细情况是怎样的? 根据屏幕尺寸和分辨率,ios现在数起来有6个版本.一,3GS:二,4s为代表:三,iphone5:四,ipad2为代表:五,ipad4 ...

  9. ios之UIPageControl

    分页控件是一种用来取代导航栏的可见指示器,方便手势直接翻页,最典型的应用便是iPhone的主屏幕,当图标过多会自动增加页面,在屏幕底部你会看到原点,用来只是当前页面,并且会随着翻页自动更新. 一.创建 ...

  10. UVa 291 The House Of Santa Claus——回溯dfs

    题意:从左下方的1开始,一笔画出圣诞老人的房子. #include <iostream> #include <cstring> using namespace std; ][] ...