有时候会将参数(返回结果)压缩(解压),加密(解密)

将json参数通过GZip压缩  Base64加密

 1 public static String gzipAndEncryption(String data){
2 String result = "";
3
4 try {
5 //请求参数的数据压缩
6 ByteArrayOutputStream out = new ByteArrayOutputStream();
7 GZIPOutputStream gzip = new GZIPOutputStream(out);
8 if (StrUtil.isEmpty(data)) {
9 log.warn("传递请求参数为空");
10 return result;
11 }
12 gzip.write(data.getBytes());
13
14 out.close();
15 gzip.close();
16
17 //加密
18 result = new BASE64Encoder().encodeBuffer(out.toByteArray());
19
20 } catch (Exception e) {
21 log.error("请求参数压缩加密失败,失败原因:{}",e);
22 }
23 log.info("请求参数压缩加密结果:" + result);
24 return result;
25 }

将返回结果Base64解密  GZip解压

 1  public static String decryptionAndUnzip(String data){
2 String result = "";
3
4 try {
5 //解密
6 ByteArrayOutputStream out = new ByteArrayOutputStream();
7 byte[] c = new Base64().decode(data);
8 //解压
9 ByteArrayInputStream in = new ByteArrayInputStream(c);
10 GZIPInputStream gunzip = new GZIPInputStream(in);
11 byte[] buffer = new byte[1024];
12 int offset = -1;
13 while ((offset = gunzip.read(buffer)) >= 0) {
14 String s = new String(buffer, StandardCharsets.UTF_8);
15 buffer = s.getBytes();
16 out.write(buffer, 0, offset);
17 }
18 result = out.toString();
19 log.info("响应参数解密解压结果:" + result);
20 out.close();
21 in.close();
22 gunzip.close();
23
24 } catch (IOException e) {
25 log.warn("响应参数解密解压失败,失败原因:{}",e);
26 }
27
28 return result;
29 }

发送post请求

 1 public static String sendHttpPost(String url,String param){
2 String result = null;
3
4 try {
5 HttpClient httpClient = new HttpClient();
6 PostMethod postMethod = new PostMethod(url);
7
8 //压缩加密参数
9 String paramStrPwd = gzipAndEncryption(param);
10
11 RequestEntity se = new StringRequestEntity(paramStrPwd,"application/json" ,"UTF-8");
12
13 postMethod.setRequestEntity(se);
14 postMethod.setRequestHeader("Content-Type","application/json");
15 postMethod.addRequestHeader("accept-encoding", "gzip");
16 postMethod.addRequestHeader("content-encoding", "gzip");
17
18 httpClient.executeMethod(postMethod);
19 String response = postMethod.getResponseBodyAsString();
20
21 //响应结果解密解压缩
22 result = decryptionAndUnzip(response);
23 } catch (Exception e) {
24 log.info("请求调用失败,请求路径:{},请求参数:{},失败原因:{}",url,param,e);
25 }
26
27 return result;
28 }

httpclient调用接口的更多相关文章

  1. httpClient调用接口的时候,解析返回报文内容

    比如我httpclient调用的接口返回的格式是这样的: 一:data里是个对象 { "code": 200, "message": "执行成功&qu ...

  2. 使用HttpClient调用接口

    一,编写返回对象 public class HttpResult { // 响应的状态码 private int code; // 响应的响应体 private String body;get/set ...

  3. 使用httpClient调用接口,参数用map封装或者使用JSON参数,并转换返回结果

    这里接口用表存起来,标记请求方式,然后接受参数,消息或者请求参数都可以, 然后先是遍历需要调用的接口,封装参数,再分别调用get与post即可,没有微服务还是得自己写 //消息转发-获取参数中对应参数 ...

  4. java通过HttpClient调用接口总结

    2.HttpClient 2.1简介: 最近看项目的代码,看到工程中有两个jar包张的很像,一个是commons.httpclient-3.1.jar,一个是httpclient4.2.1.jar,很 ...

  5. 使用httpClient调用接口获取响应数据

    转自:https://blog.csdn.net/shuaishuaidewo/article/details/81136088 import lombok.extern.slf4j.Slf4j; i ...

  6. 使用HttpClient调用第三方接口

    最近项目中需要调用第三方的Http接口,这里我用到了HttpClient. 首先我们要搞明白第三方接口中需要我们传递哪些参数.数据,搞明白参数以后我们就可以使用HttpClient调用接口了. 1.调 ...

  7. 使用HttpClient访问接口(Rest接口和普通接口)

    这里总结一下使用HttpClient访问外部接口的用法.后期如果发现有什么缺陷会更改.欢迎读者指出此方法的不足之处. 首先,创建一个返回实体: public class HttpResult { // ...

  8. springMVC、httpClient调用别人提供的接口!!!(外加定时调用)

    import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...

  9. HttpClient方式调用接口的实例

    使用HttpClient的方式调用接口的实例. public class TestHttpClient { public static void main(String[] args) { // 请求 ...

  10. 服务端调用接口API利器之HttpClient

    前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...

随机推荐

  1. [oeasy]python0070_动态类型_静态类型_编译_运行_匈牙利命名法

    动态类型_静态类型 回忆上次内容   上次了解了 帮助文档的 生成 开头的三引号注释 可以生成 帮助文档 文档 可以写成网页   python3 本身 也有 在线的帮助手册   目前的程序 提高了 可 ...

  2. 新年恭喜发财-scratch编程作品

    程序说明: <新年-恭喜发财>是一个基于Scratch平台制作的动画贺卡项目.该项目通过编程和艺术设计,展现了浓厚的中国新年(2024年为龙年)氛围,以及传统的恭喜发财祝福.动画中包含有喜 ...

  3. windows环境xampp搭建php电商项目/搭建禅道

    windows环境xampp搭建php论坛/电商项目 一,首先下载xampp https://www.apachefriends.org/zh_cn/index.html 下载之后解压到E盘或者F盘的 ...

  4. 【Java】用户在线人数统计的简单实现

    一.需求效果: 就是进入首页时能查看在线人数,没有特定要求,那我就不刷这个接口了 就进入首页加载一次 二.实现思路: 思路参考博客: https://blog.csdn.net/GitLuckyd/a ...

  5. anaconda环境下:强化学习PPO算法仿真环境库sample-factory的python完美适配版本为python3.11

    anaconda环境下:强化学习PPO算法仿真环境库sample-factory的python完美适配版本为python3.11 库sample-factory地址: https://github.c ...

  6. batch normalization的multi-GPU版本该怎么实现? 【Tensorflow 分布式PS/Worker模式下异步更新的情况】

    最近由于实验室有了个AI计算平台,于是研究了些分布式和单机多GPU的深度学习代码,于是遇到了下面的讨论: https://www.zhihu.com/question/59321480/answer/ ...

  7. docker容器挂载host宿主机的本地目录,docker容器与宿主机之间互相拷贝文件

    docker容器挂载host宿主机的本地目录,docker容器与宿主机之间互相拷贝文件 参考于: https://blog.csdn.net/weixin_37773766/article/detai ...

  8. 使用SSH连接局域网内的WSL Ubuntu

    参考: https://zhuanlan.zhihu.com/p/586283483 https://www.cnblogs.com/lidabo/p/16855975.html ========== ...

  9. 什么是snapshot isolation

    数据库常见的4种事务隔离级别: (源自:(34条消息) 8. 事务隔离级别: 总结_oyw5201314ck的博客-CSDN博客_ck事务隔离) 大多数的数据库默认的事务隔离级别是Repeatable ...

  10. tensorflow1.x——如何在C++多线程中调用同一个session会话

    相关内容: tensorflow1.x--如何在python多线程中调用同一个session会话 ================================================= 从 ...