zoukankan      html  css  js  c++  java
  • httpclient调用接口

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

    将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     }
    View Code

    将返回结果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     }
    View Code

    发送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     }
    View Code
  • 相关阅读:
    ASP.NET MVC 学习笔记 1
    安装xp遇到的问题与如何连接共享的打印机
    win8.1 安装
    AspxGridView控件的使用
    JS获取fileupload文件全路径
    正则表达式的学习
    回归起点
    Vmware ESX 5.0 安装与部署
    UITextField检测输入内容不能有空格的处理
    关于cell自动布局约束实现高度自适应问题
  • 原文地址:https://www.cnblogs.com/xuqiang7/p/11637284.html
Copyright © 2011-2022 走看看