客户端代码


/**
* 从接口获取数据
* @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推送数据的更多相关文章

  1. java接口对接——调用别人接口推送数据

    实际开发中经常会遇到要和其他平台或系统对接的情况,实际操作就是互相调用别人的接口获取或者推送数据, 当我们调用别人接口推送数据时,需要对方给一个接口地址以及接口的规范文档,规范中要包括接口的明确入参及 ...

  2. SQL Server 2000向SQL Server 2008 R2推送数据

    [文章摘要]最近做的一个项目要获取存在于其他服务器的一些数据,为了安全起见,采用由其他“服务器”向我们服务器推送的方式实现.我们服务器使用的是SQL Server 2008 R2,其他“服务器”使用的 ...

  3. WebService推送数据,数据结构应该怎样定义?

    存放在Session有一些弊端,不能实时更新.server压力增大等... 要求:将从BO拿回来的数据存放在UI Cache里面,数据库更新了就通过RemoveCallback "告诉&qu ...

  4. Flume推送数据到SparkStreaming案例实战和内幕源码解密

    本期内容: 1. Flume on HDFS案例回顾 2. Flume推送数据到Spark Streaming实战 3. 原理绘图剖析 1. Flume on HDFS案例回顾 上节课要求大家自己安装 ...

  5. SuperSocket主动从服务器端推送数据到客户端

    关键字: 主动推送, 推送数据, 客户端推送, 获取Session, 发送数据, 回话快照 通过Session对象发送数据到客户端   前面已经说过,AppSession 代表了一个逻辑的 socke ...

  6. Asp.net Core3.1+Vue 使用SignalR推送数据

    本文就简单使用 往前端页面推送消息 SignalR 是什么 SignalR是一个.NET Core/.NET Framework的开源实时框架. SignalR的可使用Web Socket, Serv ...

  7. hive向es推送数据

    第一步:首先要保证网络是通的,很多公司里子网遍布,要和运维和工程侧同事确认好网络是通的,es的地址可以通过curl es地址的方式测试一下. 第二步:下载需要的jar包,必须的是es-hadoop的包 ...

  8. 使用SignalR ASP.NET Core来简单实现一个后台实时推送数据给Echarts展示图表的功能

    什么是 SignalR ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能. 实时 web 功能使服务器端代码可以立 ...

  9. java推送数据到app--极光推送

    之前项目有用到需要把数据推送到app端 采用的是极光推送 特此把工具类和pom.xml需要的jar整理如下 pom.xml需要jar如下 <!-- 极光推送 --> <depende ...

随机推荐

  1. Lambda 表达式推演全过程

    Java 的 Lambda 表达式推演过程: 第一步:正常的类实现(外部实现),new一个对象,然后重写方法实现 public class TestLambda3 { public static vo ...

  2. Spark SQL dropDuplicates

    spark sql 数据去重 在对spark sql 中的dataframe数据表去除重复数据的时候可以使用dropDuplicates()方法 dropDuplicates()有4个重载方法 第一个 ...

  3. 光年数据分析表(seo数据监控表和爬虫数据监控表)

    http://www.wocaoseo.com/thread-307-1-1.html 光年seo培训想必很多人都知道,他们提出的数据化操作影响了很多的seo从业者,下面是他们的2个数据表,搜集于网络 ...

  4. 什么是URL标准化

    http://www.wocaoseo.com/thread-194-1-1.html RL标准化是来自于国外matt cutts的博客文章,期望能给蜘蛛一个规范化的URL作为收录的标准.举个简单的例 ...

  5. 利用GetPrivateProfileString读取配置文件(.ini)

    利用GetPrivateProfileString读取配置文件(.ini) 配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件:bool WritePrivateProfileSt ...

  6. Unity动态绑定按钮触发方法

    在使用unity制作UI的过程中,基本都需要接触到按钮,然后按钮要起作用的话,那么就需要为按钮绑定响应方法. 为按钮绑定触发的方法,我知道的有两种方法,第一种:手动使用unityEditor 绑定,另 ...

  7. 【C#】Random类中构造方法、时间种子与随机数序列的关系

    Random类 构造函数 1) Random random = new Random(); // 无参数构造函数使用系统时钟生成其种子值 然而,系统时钟取值范围有限,因此在小规模计算中,可能无法使用不 ...

  8. 聊聊redis单线程为什么能做到高性能和io多路复用到底是个什么鬼

    1:io多路复用epoll  io多路复用简单来说就是一个线程处理多个网络请求 我们知道epoll in 的事件触发是可读了,这个比较好理解,比如一个连接过来,或者一个数据发送过来了,那么in事件就触 ...

  9. 把Employees显示在页面上

    项目代码下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200429-1.zip 需求:从后台DB取出雇员数据,显示在前台页面上: ...

  10. 转载:Window配置Redis环境和简单使用

    原作:https://www.cnblogs.com/wxjnew/p/9160855.html 我自己的尝试:https://www.cnblogs.com/xiandedanteng/p/1214 ...