EntityUtils.toString(entity)处理字符集问题解决

博客主要围绕EntityUtils.toString(entity)处理字符集问题展开,提供了相关参考文章及对应链接,致力于解决该方法在处理字符集时遇到的问题。
public static String actionPostStringToSCADA(HttpBaseEntity httpBaseEntity, String requestStr) { String responseStr = null; if (httpBaseEntity != null) { String domain = httpBaseEntity.getDomain(); String port = httpBaseEntity.getPort(); String resourcePath = httpBaseEntity.getResourcePath(); // //拼接出对应的地址 // String url = new StringBuffer("http://") // .append(domain) // .append(":") // .append(port) // .append("/") // .append(resourcePath) // .toString(); //拼接出对应的地址 String url = new StringBuffer("http://") .append(domain) .append(":") .append(port) .append("/") .append(resourcePath) .toString(); //输出url和请求报文 System.out.println("actionPostStringToSCADA url:\n" + url); String requestJsonStr = requestStr; System.out.println("actionPostStringToSCADA requestJsonStr:\n" + requestJsonStr); //创建请求头,做规范 Map<String, String> headers = new HashMap<>(); // headers.put("Content-type", "text/xml;charset=UTF-8"); headers.put("Content-Encoding", "UTF-8"); headers.put("CharacterEncoding", "UTF-8"); //http post方式请求封装,数据传输格式默认为xml,获取相应的返回数据 responseStr = HttpUtils.HttpPostString( url, headers, requestJsonStr ); } return responseStr; } public static String HttpPostString(String url, Map<String, String> headers, String requestStr) { String responseStr = ""; HttpClient httpClient = null; HttpPost httpPost = null; try { if (org.springframework.util.StringUtils.hasLength(url) && url.startsWith("http://")) { httpClient = HttpClients.custom().build(); httpPost = new HttpPost(url); //组装http header httpPost.setHeader("Content-type", "text/xml;charset=UTF-8"); if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue()); } } //组装http entity if (org.springframework.util.StringUtils.hasLength(String.valueOf(requestStr))) { StringEntity entity = new StringEntity(String.valueOf(requestStr), Charset.forName("UTF-8")); entity.setContentEncoding("UTF-8"); entity.setContentType("text/xml"); if (headers != null && headers.size() > 0) { if (headers.containsKey("Content-Encoding")) { entity.setContentEncoding(headers.get("Content-Encoding")); } if (headers.containsKey("Content-Type")) { entity.setContentType(headers.get("Content-Type")); } } httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity responseEntity = response.getEntity(); responseStr = EntityUtils.toString(responseEntity); System.out.println(statusCode + " | " + responseStr); } } catch (IOException e) { e.printStackTrace(); System.out.println("error:" + e.getMessage()); } finally { if (httpPost != null) httpPost.releaseConnection(); } return responseStr; }这是原先发送http逻辑,现在要改为json发送 应该如何修改
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值