Illegal character ((CTRL-CHAR, code 31))问题排查 gzip接口返回 RestTemplate GET POST请求
Illegal character ((CTRL-CHAR, code 31))问题排查 gzip接口返回
#接口返回gzip方式
private static final String ENCODING_UTF8 = "UTF-8";
private static ObjectMapper mapper = new ObjectMapper(); private void writeResponse(HttpServletResponse response, Object outJB){
response.setHeader("Access-Control-Allow-Origin","*");
response.setContentType("text/html; charset=utf-8");
//response.setContentType("application/json; charset=utf-8");
//response.setContentType("text/json; charset=utf-8");
response.setCharacterEncoding(ENCODING_UTF8);
response.setHeader("Content-Encoding", "gzip");
GZIPOutputStream out = null;
//byte[] jsonOut = null;
String jsonOut = null;
try {
// mapper.setSerializationInclusion(Include.ALWAYS);
jsonOut = mapper.writeValueAsString(outJB);
out = new GZIPOutputStream(response.getOutputStream());
out.write(jsonOut.getBytes(ENCODING_UTF8));
out.flush();
} catch (Exception e) {
logger.error("",e);
} finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.非gzip接口请求方式
//RestTemplate POST/PUT请求
//requestVO请求参数
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-Type", "application/json;charset=UTF-8");
// requestHeaders.add("User-Agent", "mysite_web");
requestHeaders.add("connection", "Keep-Alive");
// requestHeaders.add("Accept", "application/json;version=3.0;compress=false"); RestTemplate template = new RestTemplate();
HttpEntity<String> requestEntity = new HttpEntity<>(GsonUtils.toJson(requestVO), requestHeaders);
logger.info("url=" + serverURL + "/console/test/testRemark/add" );
ResponseEntity<ResponseData> response = template.exchange(serverURL + "/console/test/testRemark/add",
HttpMethod.POST, requestEntity, ResponseData.class); ResponseData httpResult = response.getBody();
logger.info("httpResult=" + GsonUtils.toJson(httpResult)); //RestTemplate GET请求
//请求参数跟在url后面
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-Type", "application/json;charset=UTF-8");
// requestHeaders.add("User-Agent", "mysite_web");
requestHeaders.add("connection", "Keep-Alive");
// requestHeaders.add("Accept", "application/json;version=3.0;compress=false"); RestTemplate template = new RestTemplate();
HttpEntity<String> requestEntity = new HttpEntity<>(null, requestHeaders);
logger.info("url=" + serverURL + "/console/test/testRemark/add?参数1=aa&参数2=bb" );
ResponseEntity<ResponseData> response = template.exchange(serverURL + "/console/test/testRemark/add?参数1=aa&参数2=bb",
HttpMethod.GET, requestEntity, ResponseData.class); ResponseData httpResult = response.getBody();
logger.info("httpResult=" + GsonUtils.toJson(httpResult));
2.gzip接口请求方式
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; /**
* 请求接口返回gzip的http工具类
*/
public class HttpUtil { public class HttpResult { private String resCode;
private String resMsg;
private Object data; public String getResCode() {
return resCode;
} public void setResCode(String resCode) {
this.resCode = resCode;
} public String getResMsg() {
return resMsg;
} public void setResMsg(String resMsg) {
this.resMsg = resMsg;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
}
} private static final String POST = "POST";
private static final String GET = "GET";
private static final String DELETE = "DELETE";
private static final String PUT = "PUT"; private static final String ENCODEING = "UTF-8"; public static HttpResult get(String reqUrl) throws Exception{
String result = send(reqUrl, "", GET);
System.out.println("reqUrl="+reqUrl+";result="+result); ObjectMapper om = new ObjectMapper();
return om.readValue(result, HttpResult.class);
} public static HttpResult post(String reqUrl, String reqContent) throws Exception{
String result = send(reqUrl, reqContent, POST);
System.out.println("reqUrl="+reqUrl+";result="+result); ObjectMapper om = new ObjectMapper();
return om.readValue(result, HttpResult.class);
} private static String send(String reqUrl, String reqContent, String method) throws Exception {
String resContent = null; HttpURLConnection conn = null;
GZIPOutputStream out = null;
GZIPInputStream in = null;
BufferedReader reader = null;
try {
URL url = new URL(reqUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000);
conn.setReadTimeout(100000); conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);//是否自动处理重定向
conn.setRequestMethod(method);
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");//传递参数使用 &链接的表单提交方式
conn.setRequestProperty("User-Agent", "mysite_web"); conn.connect();
//java.util.zip.ZipException: Not in GZIP format
if (!method.equals(GET)) {
out = new GZIPOutputStream(conn.getOutputStream());
//发送请求数据
out.write(reqContent.getBytes(ENCODEING));
out.flush();
out.close();
} //接收返回数据
int resCode = conn.getResponseCode();
if(resCode == 200){
in = new GZIPInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in, ENCODEING));
resContent = reader.readLine();
}else{
System.out.println("服务器返回码:"+resCode);
}
} catch (Exception e) {
throw e;
} finally{
if(reader != null){
reader.close();
}
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(conn != null){
conn.disconnect();
}
}
return resContent;
}
}
Illegal character ((CTRL-CHAR, code 31))问题排查 gzip接口返回 RestTemplate GET POST请求的更多相关文章
- java.net.MalformedURLException: Illegal character in URL
在进行接口测试时,意外发现返回结果报java.net.MalformedURLException: Illegal character in URL,意思是“在URL中的非法字符”,我的参数是经过ba ...
- PHP出现iconv(): Detected an illegal character in input string
PHP传给JS字符串用ecsape转换加到url里,又用PHP接收,再用网上找的unscape函数转换一下,这样得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv转换 开始是这样用 ...
- java.lang.IllegalArgumentException: Illegal character in query at index 261
在BaseFragment中使用了LoadingPage,而LoadingPage的联网加载使用的是AsyncHttpClient.一直报java.lang.IllegalArgumentExcept ...
- 编译工程时报illegal character:\65279--转
windows对UTF-8文件进行了特殊处理,对UTF-8的文本文件自动在前面加了三个byte(EF BB BF),javac编译时,读到最前面这三个byte时报错 illegal character ...
- URL中增加BASE64加密的字符串引起的问题(java.net.MalformedURLException:Illegal character in URL)
序 昨天在做一个 Demo 的时候,因为是调用第三方的接口,採用的是 HTTP 的通信协议,依照文档上的说明,须要把參数进行加密后增加到 URL 中.可是,就是这个看似普普通通的操作,却让我着实费了非 ...
- 爬取数据时解析url时一直报错Caused by: java.net.URISyntaxException: Illegal character in query at index 823替换了所有空格和特殊字符还是无效
近日在用HttpClient访问抓取汇率时,为了省力,直接采用 String url = "http://api.liqwei.com/currency/?exchange=usd|cny& ...
- php iconv() : Detected an illegal character in input string
php iconv() : Detected an illegal character in input string_php技巧_脚本之家 https://www.jb51.net/article/ ...
- 转-java编译时error: illegal character '\ufeff' 的解决办法-https://blog.csdn.net/t518vs20s/article/details/80833061
原文链接:https://blog.csdn.net/shixing_11/article/details/6976900 最近开发人员通过SVN提交了xxx.java文件,因发布时该包有问题需要回退 ...
- curl 错误 [globbing] illegal character in range specification at pos
现象 在使用curl 进行ipv6请求的时候 curl -v "http://[1:1::1]/test.html" 发生了一个错误,报错是 [globbing] illegal ...
- 腾讯云 activeMQ Illegal character in hostname at index 7
查找问题步骤: 1. /usr/local/apache-activemq-5.9.1/data/activemq.log 看一下这个.log后缀的启动日志,可以将它下载下来再看. 先尝试修改配置文 ...
随机推荐
- LVGL 字体
一.LVGL 内置字体 LVGL有几种不同大小的内置字体,可以通过 LV_FONT_MONTSERRAT_X 定义在 lv_conf.h 中启用. 普通字体 包含所有ASCII字符,度数符号(U + ...
- vue关于this.$refs.tabs.refreshs()刷新组件,缓存
当更改了用户信息后,需要刷新页面或者组件. 1.当前组件刷新.定义一个请求用户信息的方法,在需要时调用: sessionStorage.setItem('userInfo',JSON.stringif ...
- Dinky实时计算平台
前言:Apache Flink 作为新一代的实时计算框架已经被应用到各个行业与领域,其岂存在着应用的痛点比如 FlinkSQL 在线IDE.作业提交不友好.作业无监控报警等.很大程度上说,FlinkS ...
- 【内存优化】Oracle 的SGA与Linux的shmall和shmmax的关联
查看linux下的Oracle共享内存段 [oracle@oradb ~]$ ipcs -m ------ Shared Memory Segments -------- key shmid owne ...
- 大模型高效微调详解-从Adpter、PrefixTuning到LoRA
一.背景 目前NLP主流范式是在大量通用数据上进行预训练语言模型训练,然后再针对特定下游任务进行微调,达到领域适应(迁移学习)的目的. 指令微调是预训练语言模型微调的主流范式 其目的是尽量让下游任务的 ...
- 一个list分成 list长度/step_length 向上取整个小list集合
一.具体实现方法 /** * 将一个list按照新的步长分成list长度/step_length 向上取整个小list * @param list * @param step_length * @re ...
- IPv6 — 基于邻居发现协议的通信方式
目录 文章目录 目录 前文列表 IPv6 的通信方式 NDP(Neighbor Discovery Protocol,邻居发现协议) IPv6 地址自动配置 无状态自动配置概述 前文列表 <IP ...
- 基于Python实现MapReduce
一.什么是MapReduce 首先,将这个单词分解为Map.Reduce. Map阶段:在这个阶段,输入数据集被分割成小块,并由多个Map任务处理.每个Map任务将输入数据映射为一系列(key, va ...
- 【漏洞复现】金蝶OA-EAS系统 uploadLogo.action 任意文件上传漏洞(0day)
阅读须知 此文所提供的信息只为网络安全人员对自己所负责的网站.服务器等(包括但不限于)进行检测或维护参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作.利用此文所提供的 ...
- RoslynPad的简单使用
虽说Visual Studio被我们戏称宇宙最强IDE,但是平常随手写段C#代码进行验证或者语法校验,属于牛刀小试了,显然轻量级C#编辑器更适合这种场景,目前较为流行的则是一代神器 LINQPad,但 ...