zoukankan      html  css  js  c++  java
  • httpcomponent框架MultipartEntityBuilder addTextBody中文乱码

    版本4.5.6

       String url = "https://172.16.3.50:8111/api/tts/offline";
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("User-Agent", "Mozilla/5.0 巧妙欺骗过浏览器Agent");
            httpPost.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            httpPost.addHeader("Accept-Encoding", "gzip, deflate");
            httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
    
            ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), StandardCharsets.UTF_8);
            StringBody stringBody = new StringBody("先生,这是您的咖啡,请慢慢享用",contentType);
    
    
            HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532)
                    .addPart("text",stringBody)
                    .addTextBody("speakerId", "person_2")
                    .addTextBody("speed", "1.5")
                    .addTextBody("pitch", "1.2")
                    .addTextBody("volume", "1")
    
                    .build();
    
            httpPost.setEntity(reqEntity);
            CloseableHttpResponse resp = httpClient.execute(httpPost);
    
            HttpEntity entity = resp.getEntity();
            System.out.println(entity);

    之前使用.addTextBody("text", "先生,,,")服务器端一直乱码,显示很多问号???这是字符集问题。

    在网上找到了一份答案:https://blog.csdn.net/kufeiyun/article/details/45172563

    // 使用addPart+ StringBody代替addTextBody,解决中文乱码

    // builder.addTextBody(entry.getKey(), entry.getValue());

    ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
    StringBody stringBody = new StringBody(entry.getValue(),contentType);
    builder.addPart(entry.getKey(), stringBody);

    至此解析正确

    正确:

  • 相关阅读:
    jdbc连接Sql server数据库,并查询数据
    HttpClient,post请求,发送json,并接收数据
    SQL SERVER存储过程一
    HttpClient,get请求,发送并接收数据
    工作中操作数据库实例
    存储过程的实例(公司)
    eclipse发布项目后,项目所在的位置
    SQLSERVER存储过程基本语法
    SAXReader解析
    导包
  • 原文地址:https://www.cnblogs.com/passedbylove/p/12017712.html
Copyright © 2011-2022 走看看