背景
寫一個網關功能的自動化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();