背景
写一个网关功能的自动化case,需要发送一个http post 请求,并且请求头header带上几个参数:Accept-Encoding 和 X-Encrypt 参数 分别表示的含义是 压缩 和 加密的两个参数。带上这个参数之后返回的response会按照设定的参数值的模式进行加密和压缩。
使用的代码
//首先初始化了一个httpclient 用来执行httppost 请求
client = HttpClients.createDefault();
//设置header
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept-Encoding","GLZip");
httpPost.setHeader("X-Encrypt","1");
执行报错:
org.apache.http.HttpException: Unsupported Content-Coding:GLZip
报错分析
报错原因:httpclient 自身对内容 content-coding 默认存在规范的要求,只能gzip ...一些格式,向上面代码的glzip属于自定义的不支持的范围,所以执行出现报错
解决办法
使用下面的code 可以简单解决,避免掉coding内容的校验
HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder.disableContentCompression();
client = clientBuilder.build();