处理爬取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;
    }
相关推荐
万少4 分钟前
公测期 0 元/月!商汤 SenseNova 免费 Token 再不领就没了
前端·javascript·后端
Hello--_--World6 分钟前
Webpack:Webpack 核心配置、什么是 Loader? 什么是plugin?webpack 构建流程
前端·webpack·node.js
优联前端7 分钟前
什么是 GEO?SEO对比GEO,如何做好 GEO?怎么验证 GEO 效果?
前端·人工智能·用户体验·geo·seo优化·优联前端
时间不早了sss8 分钟前
Python处理文档
开发语言·前端·python
Json____9 分钟前
前端入门练习题集-HTML/CSS/JS实战小项目15个
前端·css·html
feng_you_ying_li9 分钟前
linux之文件系统(2)
linux·运维·服务器
手打猪大屁11 分钟前
使用claude code 接入deepseek-v4pro
linux·windows·ai·deepseek·claude code
科研小白_13 分钟前
【第二期:MATLAB点云处理基础】KD树与点云邻域搜索
java·前端·人工智能
小江的记录本13 分钟前
【MySQL】《MySQL基础架构 面试核心考点问答清单》
前端·数据库·后端·sql·mysql·adb·面试
爱网络爱Linux14 分钟前
华为HCIP——BGP 基础配置
服务器·前端·华为·hcip·hcip datacom·华为数通认证