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

问题如图

处理代码如下

 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);

相当于加上

 HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>(json, headers);

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

以下为处理html模板页内容

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;
    }

相关推荐

  1. vscode Run Code输出出现中文情况问题解决方案

    2024-04-06 03:08:11       27 阅读
  2. Qt读写ini文件,出现中文问题

    2024-04-06 03:08:11       68 阅读
  3. jd-gui反编译出现中文问题

    2024-04-06 03:08:11       22 阅读
  4. 为什么出现

    2024-04-06 03:08:11       32 阅读
  5. less中文问题

    2024-04-06 03:08:11       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 03:08:11       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 03:08:11       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 03:08:11       87 阅读
  4. Python语言-面向对象

    2024-04-06 03:08:11       96 阅读

热门阅读

  1. Spring注入方式解析与实践

    2024-04-06 03:08:11       34 阅读
  2. Python笔记|列表推导式

    2024-04-06 03:08:11       41 阅读
  3. 设计模式:原型模式

    2024-04-06 03:08:11       43 阅读
  4. deepspeed学习-多机all_reduce

    2024-04-06 03:08:11       31 阅读
  5. Kubernetes学习笔记6

    2024-04-06 03:08:11       42 阅读
  6. 威胁建模与网络安全测试方法

    2024-04-06 03:08:11       42 阅读
  7. 2024.3.24力扣每日一题——零钱兑换

    2024-04-06 03:08:11       35 阅读
  8. 2024/4/2 HarmonyOS学习笔记一TS数据类型

    2024-04-06 03:08:11       36 阅读