zoukankan      html  css  js  c++  java
  • httpclient接口返回结果中文显示问号

    现象:

    解决办法:StringEntity,要加个UTF-8参数

    package com.sf.autotest.common;
    
    import com.alibaba.fastjson.JSONObject;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.core.io.ClassPathResource;
    
    import javax.sound.midi.Soundbank;
    import java.io.InputStream;
    import java.io.IOException;
    
    import java.nio.charset.StandardCharsets;
    import java.util.HashMap;
    import java.util.Set;
    
    
    public class SendData {
        private static Logger log = LoggerFactory.getLogger(SendData.class);
        public static HashMap changeData;
    
        public static String sendXml(String resourcePath) throws IOException {
            HashMap<String, String> changeData = new HashMap<>();
            changeData.put("{{eno}}", "住院号");
            changeData.put("{{pid}}", "患者号");
            Set<String> keySet = changeData.keySet();
    
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(sfContant.auditcenter_url + "/api/v1/auditcenter");
            String xml = "";
            try (InputStream input = new ClassPathResource(resourcePath).getInputStream()) {
                xml = IOUtils.toString(input, StandardCharsets.UTF_8);
            }
            for (String key : keySet) {
                xml = xml.replace(key, changeData.get(key));
            }
            log.info("请求{}的入参为:
    {}", sfContant.auditcenter_url, xml);
            StringEntity requestEntity = new StringEntity(xml,"UTF-8");
            requestEntity.setContentType("text/plain");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            CloseableHttpResponse respose = httpClient.execute(httpPost);
            HttpEntity entity = respose.getEntity();
            String resBody  = EntityUtils.toString(entity);
    //        System.out.println(resBody);
    
    //        log.info("
    {}",EntityUtils.toString(entity,"utf-8"));
            return resBody;
        }
    
    
    
        public static void main(String[] args) throws IOException {
            String s = SendData.sendXml("audit.xml");
            System.out.println(s);
    
    
    
        }
    }

    特别注意:

    EntityUtils.toString(entity),只能获取一次,再次访问会报错 流关闭

  • 相关阅读:
    JavaScript实现类的private、protected、public、static以及继承
    OSS网页上传和断点续传(STSToken篇)
    OSS网页上传和断点续传(OSS配置篇)
    Linq sum()时遇到NULL
    SQLSERVER事务日志已满 the transaction log for database 'xx' is full
    笔记本高分辨软件兼容问题,字体太小或模糊
    H5上传图片之canvas
    An error occurred while updating the entries. See the inner exception for details.
    无限级结构SQL查询所有的下级和所有的上级
    SQLserver 进程被死锁问题解决
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/13202766.html
Copyright © 2011-2022 走看看