/*
* 创建日期 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. 剑指offer——把字符串转换成整数(c++)

    题目描述请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不能使用atoi或者其他类似的库函数. 示例 1:输入: " -42"输出: -42解释: 第一个非空 ...

  2. Google Colab调用cv2.imshow奔溃

    当我在Google Colab运行如下代码 import cv2 import numpy as np image = cv2.imread('a.jpg') cv2.imshow('original ...

  3. 想转行做web前端工程师,必学这5大技能!知道是那些吗?

    web前端工程师是近几年才发展出来的新兴职业,也是目前火爆且高薪的职业. 大需求的市场环境下,出现了越来越多的人群转行做web前端工程师,如设计师.后台程序员.网虫.大学其他专业.策划.编辑等等. 要 ...

  4. java web.xml被文件加载过程及加载顺序小结

    web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...

  5. CPP-网络/通信:COM

    ))//打开串口 { ) { CloseCom();//关闭串口 break; } //添加处理代码. } //最后关闭串口 CloseCom();//关闭串口

  6. std::ios::sync_with_stdio和tie()——给cin加速

    平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(fal ...

  7. 【贪心】「poj1328」Radar Installation

    建模:二维转一维:贪心 Description Assume the coasting is an infinite straight line. Land is in one side of coa ...

  8. 【计算机网络】DNS的作用以及修改DNS的方法

    1.DNS的作用及修改DNS的方法 1.1.DNS的作用 DNS就是将域名映射成ip的分布式数据库服务器,它的作用如下图: 1.2.修改DNS的方法 常用的DNS服务器 1.114.114.114.1 ...

  9. 自定义View画一条线

    #import "PublishContextView.h" @implementation PublishContextView -(void)drawRect:(CGRect) ...

  10. linux c中需要记住的东西

    1.记住常见的字符的ASCII值 a------------97     b------------98     A------------65     B------------66     空格' ...