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

将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. Vue仿微信PC客户端

    一个模仿PC微信客户端的纯前端vue项目 项目地址 github: https://github.com/TomHusky/vue-wechat-demo gitee: https://gitee.c ...

  2. python raise异常处理

    python raise异常处理 一般最简单的异常处理是try  except: try: f = open('test.txt') except Exception as e: print(e) f ...

  3. 【DataBase】MySQL 13 分组查询

    视频参考自:P59 - P68 https://www.bilibili.com/video/BV1xW411u7ax 分组查询 GROUP BY -- group by 子句 -- 要注意!grou ...

  4. 2018年视频,路径规划:层次化路径规划系统——hierarchical pathfinding system —— Hierarchical Dynamic Pathfinding for Large Voxel Worlds (续)

    前文: 2018年视频,路径规划:层次化路径规划系统--hierarchical pathfinding system -- Hierarchical Dynamic Pathfinding for ...

  5. 全球最大开源模型Grok-1 —— 马斯克 —— 自然语言大模型

    当前世界上参数最大的开源大语言模型Grok-1,参数权重大小为296GB,即3140亿参数,远远超过了OpenAI的GPT-3.5模型. 该模型采用的3140亿参数的MoE模型,在给定token上的激 ...

  6. 大语言模型(LLM)运行报错:module ‘streamlit‘ has no attribute ‘chat_message‘

    参考: https://blog.csdn.net/weixin_45748921/article/details/134645308 问题在于版本不匹配,深究一下为什么各个版本软件不匹配,发现原因是 ...

  7. Ubuntu 18.04.4 导入docker镜像,启动镜像,保存容器为镜像,导出镜像

    1.  查看  docker 版本 sudo docker version 2. 查看本地库中的镜像 sudo docker images 3.   查看  正在运行的  容器 sudo docker ...

  8. stm32 F103C8T6 4x4矩阵键盘使用

    首先感谢 江科大 的stm32入门课程 受益匪浅.推荐有兴趣的朋友去看看. 先看看我用的矩阵键盘是啥样的(很常见的一种) 接线如图(其他型号根据自己需求接上GPIO口) 代码基于stm大善人的代码修改 ...

  9. 深度解读昇腾CANN小shape算子计算优化技术,进一步减少调度开销

    摘要:Host调度模式下,GE将模型中算子的执行单元划分为Host CPU执行与Device(昇腾AI处理器)执行两大类. 本文分享自华为云社区<深度解读昇腾CANN小shape算子计算优化技术 ...

  10. python增删查改实例

    本文介绍一个实例,即删除数据库中原有的表格TEST1,新建一个表格TEST2,并在TEST2中插入3行数据.插入数据以后,查询出ID=3的数据,读出,最后将其删除. 结果: 代码: ''' impor ...