处理爬取html内容调用RestTemplate方法远程调接口会出现中文乱码问题

问题如图

处理代码如下

java 复制代码
 RestTemplate restTemplate = new RestTemplate();
            String url1 = SystemParam.getValue("interface");
            System.out.println("htmlContent----"+htmlContent);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>(json, headers);
            String result = restTemplate.postForObject(url1, requestEntity, String.class);

相当于加上

java 复制代码
 HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>(json, headers);

使用 HttpEntity 来发送请求实体,可以使用 HttpEntity 将 JSON 字符串发送到服务端,并且确保字符集为 UTF-8

以下为处理html模板页内容

java 复制代码
public static String fetchTemplateHtml(String reporturl, String siteid, String nowDate) {
        String templateHtml = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            String url = 填写所要获取的地址接口,跳转html的接口以及接口内要有数据;下面就是处理解析数据然后放到对应的获取参数的位置,我这里处理的是不是通过ajax获取的参数,是通过原生,后端给前端通过model.addAttribute传递参数
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Accept-Encoding", "UTF-8");
            httpGet.setHeader("Content-Type", "application/json; charset=UTF-8");
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                templateHtml = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return templateHtml;
    }
相关推荐
晚风吹长发几秒前
初步理解Linux中的进程间通信以及管道通信
linux·运维·服务器·c++·进程·通信
可爱又迷人的反派角色“yang”3 分钟前
K8s(六)
linux·运维·云原生·容器·kubernetes
开开心心_Every3 分钟前
无广告输入法推荐:内置丰富词库免费皮肤
服务器·前端·学习·决策树·edge·powerpoint·动态规划
wheeldown1 小时前
【Linux】 Linux网络编程入门:Soket编程详解
linux·运维·网络
卓怡学长1 小时前
m111基于MVC的舞蹈网站的设计与实现
java·前端·数据库·spring boot·spring·mvc
C_心欲无痕6 小时前
前端实现水印的两种方式:SVG 与 Canvas
前端·安全·水印
zfxwasaboy8 小时前
DRM KMS 子系统(4)Planes/Encoder/Connector
linux·c语言
暮色_年华9 小时前
随想 2:对比 linux内核侵入式链表和 STL 非侵入链表
linux·c++·链表
尾善爱看海9 小时前
不常用的浏览器 API —— Web Speech
前端