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),只能获取一次,再次访问会报错 流关闭

  • 相关阅读:
    删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e
    设顺序表中的数据元素递增有序,试着写一算法,将x插入到顺序表上的适当位置上,以保持该表的有序性。
    数据结构-顺序表基本操作的实现(含全部代码)【转】
    【转】结构体指针
    结构体(第十四章 )
    线性表
    第二章 c语言概述
    时间复杂度
    软件质量与测试 黑盒测试
    软件质量保证与测试 基本内容
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/13202766.html
Copyright © 2011-2022 走看看