Java(25):HttpClient发起请求中文数据乱码问题解决

原因:再做接口自动化测试过程中,发现发送POST请求时,请求成功后,新增的角色描述有中文,新增成功后出现乱码。

解决方法:在POST方法初始化StringEntity时指定UTF-8

原来代码如下:

 public static HttpClientResult doPost(String url, String jsonstr,String token) throws Exception {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse=null;
        String result="";
        try {
            //1.创建httpClient对象
            //httpClient = HttpClients.createDefault();
            httpClient =createHttpsClient(filePath,passWord);
            //2.创建httpPost对象
            HttpPost httpPost = new HttpPost(url);
            //2.1对象设置请求头
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.setHeader("Authorization",token);
            //2.2对象设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIME_OUT).setConnectTimeout(CONNECT_TIME_OUT).build();
            httpPost.setConfig(requestConfig);
            //2.3对象设置请求参数
            //httpPost.setEntity(new StringEntity(json.toString()));
            httpPost.setEntity(new StringEntity(jsonstr));
            //3.使用httpClient发起请求并响应获取repsonse
            return getHttpClientResult(httpResponse,httpClient,httpPost);
            /*
            httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            //4.解析响应,获取数据
            //判断状态码是否是200
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(entity,ENCODING);
            }
        }
        finally {
            //5.释放资源
            release(httpResponse,httpClient);
        }

    }

修改如下,在初始化StringEntity时指定UTF-8:

httpPost.setEntity(new StringEntity(jsonstr,"UTF-8"));

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
出现乱码的原因可能有很多,这里列举几种可能的解决办法: 1. 设置正确的字符编码 在使用HttpClient发送请求时,需要设置正确的字符编码,否则在接收响应时可能会出现乱码。可以通过设置请求头中的Content-Type来指定编码方式,例如设置为UTF-8: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); ``` 2. 使用StringEntity传输数据 如果使用HttpClient发送POST请求,并且需要传输数据,可以使用StringEntity来设置请求数据,例如: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8"); httpPost.setEntity(stringEntity); ``` 3. 检查响应头中的Content-Type 在接收响应时,需要检查响应头中的Content-Type是否正确,例如: ``` HttpResponse httpResponse = httpClient.execute(httpPost); Header contentTypeHeader = httpResponse.getEntity().getContentType(); if (contentTypeHeader != null) { String contentType = contentTypeHeader.getValue(); if (contentType.contains("charset=GBK")) { // 使用GBK编码解析响应数据 responseStr = EntityUtils.toString(httpResponse.getEntity(), "GBK"); } else { // 使用默认编码解析响应数据 responseStr = EntityUtils.toString(httpResponse.getEntity()); } } ``` 4. 使用ByteArrayEntity传输数据 如果使用HttpClient发送POST请求,并且需要传输二进制数据,可以使用ByteArrayEntity来设置请求数据,例如: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/octet-stream"); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bytes); httpPost.setEntity(byteArrayEntity); ``` 5. 检查响应数据是否压缩 如果响应数据是压缩格式(如gzip),需要先解压缩再解析数据,例如: ``` HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); if (entity.getContentEncoding() != null && "gzip".equalsIgnoreCase(entity.getContentEncoding().getValue())) { instream = new GZIPInputStream(instream); } responseStr = EntityUtils.toString(entity, "UTF-8"); } ``` 以上是几种可能的解决办法,具体需要根据实际情况进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值