使用HttpClient实现对第三方服务器的请求并接受返回数据
/*
* 创建日期 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实现对第三方服务器的请求并接受返回数据的更多相关文章
- java 接口中模拟浏览器 请求webservice 接受返回数据
使用HttpClient 所需jar:commons-codec-1.9.jar,commons-httpclient-3.1.jar try { HttpClient client = new Ht ...
- 使用new Image()可以针对单单请求,不要返回数据的情况
使用new Image()可以针对单单请求,不要返回数据的情况,比如我这里写了一个Demo,请求百度的Logo一个示例: <html> <head> </head> ...
- httpclient post请求带参数返回数据乱码问题解决
客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...
- Fiddler -工具使用介绍(附:拦截请求并修改返回数据)(转)
一.Fiddler 介绍 Fiddler 是一个使用 C# 编写的 http 抓包工具.它使用灵活,功能强大,支持众多的 http 调试任务,是 web.移动应用的开发调试利器. 1,功能特点 同 H ...
- axios 跨域请求允许带cookie,则服务器Access-Control-Allow-Origin应设置为具体域名,否则请求无法获得返回数据
1.通过允许跨域访问实现了跨域请求,但为了使每个请求带上session信息,我设置了withCredentials ,即: axios.defaults.withCredentials = true ...
- okhttp 通过网关请求服务端返回数据
1.启动类代码 package com.tycoon.service; import org.springframework.boot.SpringApplication; import org.sp ...
- PHP通过curl向其它服务器发请求并返回数据
在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /* * 演示通过HttpClie ...
- android中用get和post方式向服务器提交请求
通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...
随机推荐
- 按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash
转自:http://www.eoeandroid.com/thread-251598-1-1.html 好像有很多朋友都碰到过这个问题,即在真机调试时,按hone键返回桌面,再回到app时,app会c ...
- openstack 存储节点按照报错Device /dev/sdb not found (or ignored by filtering).
root@dell-PowerEdge-T30:~# pvcreate /dev/sdb Device /dev/sdb not found (or ignored by filtering).首页 ...
- 如何让浏览器关闭后session失效
llzzcc66 知道合伙人数码行家 推荐于2018-08-10 如果用户不点击网站的“退出”链接,而直接关闭浏览器(或者强制关闭浏览器进程.死机等),服务器无法处理用户退出网站的请求,解决方式如 ...
- python中的循环语句-01
接触python已经一周时间,下面针对这一周python学习的情况做一梳理: 1)单行注释,使用一个#:多行注释,使用三个单引号(或者三个双引号)如: name = input("name: ...
- 量化投资,你需要了解的A股财务数据
摘要:基本面量化是应用量化研究领域的重头戏,财务数据的整理和加工是基本面量化的第一步.本文梳理了财务数据的基本知识,包括报表类型.数据来源.调整更正和使用原则等,并给出了单季度和TTM数据的计算流程. ...
- web资源持续更新----20150213
响应式设计创意收集网站:http://mediaqueri.es css禅意花园 http://www.csszengarden.com/
- VUE2中axios的使用方法
一,安装 npm install axios 二,在http.js中引入 import axios from 'axios'; 三,定义http request 拦截器,添加数据请求公用信息 axio ...
- 【OS_Linux】清空终端屏幕的方法
使用clear命令或Ctrl+L组合键来清空终端屏幕,它不是真正的清空了,而是给人以错觉,当向上滚动鼠标后发现屏幕的内容依然在. 使用printf '\033c'才真正意义上清空了屏幕,它同DOS下的 ...
- 使用 HTML5 Geolocation 构建基于地理位置的 Web 应用学习网站分享
HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...
- Linux服务器的弱口令检测及端口扫描
一.弱口令检测--John the Ripper John the Ripper工具可以帮助我们扫描出系统中密码安全性较低的用户,并将扫描后的结果显示出来. 1.安装John the Ripper: ...