/*
* 创建日期 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. 【Web应用-大文件部署】上传超过 2M 的文件到 Azure PHP 网站失败

    问题描述 上传超过 2M 的文件到 Azure PHP 网站失败. 问题分析 由于 PHP 本身默认上传文件的上限是 2M,所以当上传超过2M的文件时会报错. 解决方法 根据以下步骤进行配置: 在 s ...

  2. sql server 2008怎么设置不允许windows身份验证

  3. (一)mybatis之JDBC介绍

    前言:为什么在学mybatis之前要先了解JDBC呢?因为mybatis是以ORM模型为中心思想的框架,而所有的ORM模型都是基于JDBC进行封装的,不同的ORM模型对JDBC封装的强度是不一样的. ...

  4. ulrlib案例-爬取百度贴吧

    1.任务需求 百度贴吧有很多主题,每个主题下的网页存在很多分页.爬取不同的主题,并下载每个主题下的多页网页. 输入贴吧名称,下载相应贴吧的多页网页,设置最多下载50页. 2.分析网页 访问不同的百度贴 ...

  5. JS常用操作节点的方法

    js常见的创建dom节点的方法有 createElement() 创建一个元素节点 => 接收参数为string类型的nodename createTextNode() 创建一个文本节点 =&g ...

  6. C03 程序逻辑

    程序逻辑 运算符 顺序结构 选择结构 循环结构 运算符 赋值运算符:= 比较运算符:>.<.==. >=.<=.!= 逻辑运算符:&&.||.! 顺序结构 在C ...

  7. 【二分】bestcoder p1m2

    模型的转化和二分check的细节挺不错的 Problem Description 度度熊很喜欢数组!! 我们称一个整数数组为稳定的,若且唯若其同时符合以下两个条件: 数组里面的元素都是非负整数. 数组 ...

  8. Web框架之Django_02基本操作(Django项目启动配置、数据库连接、orm、增删改查)

    摘要: Django项目简单现实过程 pycharm连接数据库 Django之orm简单操作增删改查 一.新建Django项目.配置.设置: 新建Django项目:(为了熟悉Django操作,暂时全部 ...

  9. uboot顶层mkconfig分析

    GNU make:http://www.gnu.org/software/make/manual/make.html#Rules 为了便于理解把uboot中的Makefile配置部分弄出来便于理解,这 ...

  10. 【LeetCode】Symmetric Tree(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...