httpclient post推送数据
客户端代码
/**
* 从接口获取数据
* @param url 服务器接口地址
* @param json 传入的参数 若获取全部,此项为空
* @return 返回查询到的数据
* @throws HttpException
* @throws IOException
*/
public JSONObject post(String url, String json) throws HttpException, IOException{
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
StringEntity se = new StringEntity(json, Charset.forName("UTF-8"));
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if (response != null && response.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
JSONObject resultJsonObject = JSONObject.parseObject(result);
return resultJsonObject;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
测试方法
//本地tomcat
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCarAll";//获取全部数据
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCar";//获取某一条记录
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/postCarList";//推送数据
@Test
public void testPostList() throws IOException, HttpException {
JSONArray jsonArray = new JSONArray();
//批量推送数据
for (int i = 1; i < 3; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("carType", "测试00"+i);
//Integer modifyType 1增加 2更新
jsonObject.put("modifyType", new Integer(1));
jsonArray.add(jsonObject);
}
//推送一条数据
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("carSize", 4);
// jsonObject.put("carType", "测试3");
// jsonObject.put("keepRequirement", "无");
// jsonObject.put("keepTitle", "无");
// jsonObject.put("keepPrice", 80.0);
// jsonObject.put("repairePrice", 80.0);
// jsonObject.put("modifyType", new Integer(1));
// jsonArray.add(jsonObject);
String string = JSONArray.toJSONString(jsonArray);
JSONObject post = post(url, string);
System.out.println(post);
}
服务器方法
/**
* 推送接口,支持一条或者多条数据
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("/postCarList")
@ResponseBody
public JSONObject postCarList(HttpServletRequest request, HttpServletResponse response) throws IOException {
JSONObject jsonObject = new JSONObject();
request.setCharacterEncoding("UTF-8");
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
String jsonStr = null;
StringBuilder result = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null){
result.append(jsonStr);
}
}catch (Exception e){
e.printStackTrace();
}
reader.close();
JSONArray jsonArray = JSONArray.parseArray(result.toString());
try {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = (JSONObject) jsonArray.get(i);
Car car = new Car();
car.setCarType(o.getString("carType"));
car.setLastTime(DateUtils.stringTodate(o.getString("lastTime"), null));
car.setCarSize(o.getInteger("carSize"));
car.setKeepRequirement(o.getString("carType"));
car.setKeepTitle(o.getString("keepTitle"));
car.setKeepPrice(o.getDouble("keepPrice"));
car.setRepairePrice(o.getDouble("repairePrice"));
if (o.getInteger("modifyType") == null){
jsonObject.put("msg", "Post失败,未指定修改类型");
jsonObject.put("code", -1);
return jsonObject;
}
int modifyType = o.getInteger("modifyType");
//增加
if (modifyType == 1){
carService.save(car);
}
//更新
else if (modifyType == 2){
car.setCarId(o.getInteger("carId"));
carService.update(car);
}else {
jsonObject.put("msg", "Post失败,修改类型指定类型错误");
jsonObject.put("code", -1);
return jsonObject;
}
}
jsonObject.put("msg", "Post成功");
jsonObject.put("code", 1);
}catch (Exception e){
e.printStackTrace();
}
return jsonObject;
}
httpclient post推送数据的更多相关文章
- java接口对接——调用别人接口推送数据
实际开发中经常会遇到要和其他平台或系统对接的情况,实际操作就是互相调用别人的接口获取或者推送数据, 当我们调用别人接口推送数据时,需要对方给一个接口地址以及接口的规范文档,规范中要包括接口的明确入参及 ...
- SQL Server 2000向SQL Server 2008 R2推送数据
[文章摘要]最近做的一个项目要获取存在于其他服务器的一些数据,为了安全起见,采用由其他“服务器”向我们服务器推送的方式实现.我们服务器使用的是SQL Server 2008 R2,其他“服务器”使用的 ...
- WebService推送数据,数据结构应该怎样定义?
存放在Session有一些弊端,不能实时更新.server压力增大等... 要求:将从BO拿回来的数据存放在UI Cache里面,数据库更新了就通过RemoveCallback "告诉&qu ...
- Flume推送数据到SparkStreaming案例实战和内幕源码解密
本期内容: 1. Flume on HDFS案例回顾 2. Flume推送数据到Spark Streaming实战 3. 原理绘图剖析 1. Flume on HDFS案例回顾 上节课要求大家自己安装 ...
- SuperSocket主动从服务器端推送数据到客户端
关键字: 主动推送, 推送数据, 客户端推送, 获取Session, 发送数据, 回话快照 通过Session对象发送数据到客户端 前面已经说过,AppSession 代表了一个逻辑的 socke ...
- Asp.net Core3.1+Vue 使用SignalR推送数据
本文就简单使用 往前端页面推送消息 SignalR 是什么 SignalR是一个.NET Core/.NET Framework的开源实时框架. SignalR的可使用Web Socket, Serv ...
- hive向es推送数据
第一步:首先要保证网络是通的,很多公司里子网遍布,要和运维和工程侧同事确认好网络是通的,es的地址可以通过curl es地址的方式测试一下. 第二步:下载需要的jar包,必须的是es-hadoop的包 ...
- 使用SignalR ASP.NET Core来简单实现一个后台实时推送数据给Echarts展示图表的功能
什么是 SignalR ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能. 实时 web 功能使服务器端代码可以立 ...
- java推送数据到app--极光推送
之前项目有用到需要把数据推送到app端 采用的是极光推送 特此把工具类和pom.xml需要的jar整理如下 pom.xml需要jar如下 <!-- 极光推送 --> <depende ...
随机推荐
- 5. java 的类和对象
1.什么是类 类 :是一组相关属性和行为的集合.可以看成是一类事物的模板,使用事物的属性特征和行为特征来描述该类事物.现实中,描述一类事物:属性 :就是该事物的状态信息.行为 :就是该事物能够做什么. ...
- springboot之对之前的补充
Spring Cloud 初级 一. Spring Boot 回顾 1 什么是 Spring Boot? Spring Boot 是在 Spring 的基础之上产生的(确切的说是在 Sprin ...
- 焦大:seo思维进化论(番外)
http://www.wocaoseo.com/thread-54-1-1.html 我已经在博客说了学seo研究算法是愚蠢的行为,但是很多人仍旧来问se的算法问题,其中最多的就是问TF-IDF算法, ...
- 能卖课 会带货的CRMEB知识付费系统v1.30来了
CRMEB知识付费系统是众邦科技在疫情肆虐,国家危难时开源发布的一款产品,它的诞生是众邦人爱国情怀的一次释放,更是众邦人用技术为人们带来美好生活的一次有效实践. 知识付费系统从2020年3月发布v1. ...
- java中equals与hashCode的重写问题
这几天有一个朋友问我在重写equals和hashCode上出现了问题,最后我帮她解决了问题,同时也整理出来分享给大家 现上Object的equals与HashCode的代码 public boolea ...
- Arbitrary-Oriented Object Detection with Circular Smooth Label(ECCV2020,旋转目标检测)
论文链接:https://arxiv.org/abs/2003.05597 code:https://github.com/Thinklab-SJTU/CSL_RetinaNet_Tensorflow ...
- HDU-4417-Super Mario(线段树+离线处理)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- B - The Staircases (dp)
One curious child has a set of N little bricks. From these bricks he builds different staircases. St ...
- Codeforces1247D Power Products 暴力+优化
题意 给定数组\(a(\left| a \right|\leq 10^5)\)和整数\(k(2\leq k \leq 100)\),问满足一下条件的二元组\(<i,j>\)的数目: \(1 ...
- iptables实用知识 ,一文学会配置linux防火墙
目录 1.防火墙的概念 2. linux防火墙 3.linux数据包处理流程 3.1 linux 防火墙将以上流程,固定区分为5个流程节点 3.2 数据流程 4 linux防火墙的实现机制 4.1 i ...