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

将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教您玩转linux 010216 随机诗词 fortunezh

    我们来回顾一下 上一部分我们都讲了什么? 下载fortune 输出重定向到cowsay 多重输出重定向 fortune的细节 有没有中️文的fortune呢 # 搜索一下fortune apt sea ...

  2. 第三节 JMeter安装及配置

    1.官网地址下载 (1)JDK:https://www.oracle.com/cn/java/technologies/downloads/,下载1.8版本以上的,最好下载最新版本(本次下载本次下载了 ...

  3. 免费正版 IntelliJ IDEA license 详细指南

    一.前言 IntelliJ IDEA 一直是我非常喜欢的 IDE 自从用上之后就回不了头了,但是 Ultimate 版本的费用十分昂贵,其实 JetBrains 自己就提供了6种免费申请授权的方式:本 ...

  4. Mysql函数10-IF

    IF函数用于判断条件是否成立,成立则执行命令1,不成立则执行命令2. 1.sql查询出一列create_time select create_time from goods where id=65 2 ...

  5. 【Spring Data JPA】04 JPQL和原生SQL

    @Transactional注解 让Spring处理事务 不需要自己每次都手动开启提交回滚 FINDONE & GETONE的区别? findone是立即加载 getone是延迟加载,配合事务 ...

  6. 【Spring-Security】Re13 Oauth2协议P3 整合JWT

    视频地址: https://www.bilibili.com/video/BV12D4y1U7D8?p=44 有用到Redis存储JWT,头疼每次找Windows版的 https://github.c ...

  7. 《Bitcoin: A Peer-to-Peer Electronic Cash System》 中本聪写的比特币白皮书

    网址: https://bitcoin.org/bitcoin.pdf =============================================================

  8. 老版本mujoco: mujoco 1.31 1.40 1.50 2.00 版本下载地址

    下载地址: https://www.roboti.us/download.html 激活码下载地址: https://www.roboti.us/file/mjkey.txt 安装教程: https: ...

  9. 最短小精悍的js数组打乱顺序

          let number = [1, 45, 13, 17];       // 封装一个打乱数组的方法       function getarr(arr) {         return ...

  10. Ubuntu搭建ssh服务器

    Ubuntu安装后默认只有ssh客户端,即只能在Ubuntu内去连接其他ssh服务器,若想实现我们的Ubuntu被其他主机远程连接,则需要自己去安装ssh服务端. 安装 进入Ubuntu18.04,打 ...