在近期做的项目中,提供了一个对外的外部接口和一个调用示例。当其他人员调用时反馈了一个问题,传递的中文参数乱码,变成了 ? 。
参数传递示例:
String encryptStr = RSAManager.encryptPrivateStrBC(String.valueOf(currentTime), pr);
//加密参数
String b = "{\"serverCode\":\"20101728815\",\"page\":\"1\",\"perPage\":\"20\",\"fields\":\"\"," +
"\"busParams\":{\"\":\"\"}," +
"\"filters\":[{\"fieldName\":\"\",\"compare\":\"\",\"fieldValue\":\"\",\"type\":\"\"}]," +
"\"orderBy\":\"\",\"priTime\":\""+encryptStr+"\"," +
"\"queryDbType\":\"mysql\",\"systemId\":\"ztgl-app-0c2dea94-01ff-480f-aa21-496ac93b2f88\"}";
String a = SM2Utils.encrypt(b, "你的加密公钥");
//执行
HttpClient httpClient = null;
HttpPost httpPost = null;
httpClient = new DefaultHttpClient();
// httpPost = new HttpPost("http://msgateway.local.js.sgcc.com.cn:6001/dwfxrj-dataserver/sync/ztExternal");
httpPost = new HttpPost("http://msgateway.local.js.sgcc.com.cn:6001/dwfxrj-dataserver/externalInterface/external");
// httpPost.setHeader("Cookie", "");
//创建一个提交数据的容器F
List<BasicNameValuePair> parames = new ArrayList<>();
parames.add(new BasicNameValuePair("params", a));
//封装容器到请求参数中
HttpEntity entity = new UrlEncodedFormEntity(parames);
//设置请求参数到post请求中
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String content = EntityUtils.toString(response.getEntity());
System.out.println("执行结果---> " + content);
JSONObject object = JSONObject.parseObject(content);
String data = object.getString("data");
System.out.println("数据解密--->" + SM2Utils.decrypt(data, "你的解密私钥"));
在参数传递之前都是经过国密2加密过后的,传递过去的应该都是数字和字母不应该会产生乱码的问题。
而且对方传递的参数打印出来之后也没有乱码。
那么,乱码问题就有可能出现在了将参数封装到容器的这个位置。
所以,在这个位置加上了字符编码格式,问题解决。