使用httpClient 发送post请求 但是参数全部被转换成JSON格式,根据url传递过去后,如何将其转换为String类型
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
int result=0;
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
for( Map.Entry<String,String> entry : params.entrySet() ){
pairList.add(new BasicNameValuePair((String) entry.getKey(),(String) entry.getValue()));
}
CloseableHttpResponse httpResponse = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairList,"utf-8"));
httpResponse = httpClient.execute(httpPost);
if( httpResponse != null && httpResponse.getStatusLine().getStatusCode()==200 ){
return 0;
}
HttpEntity httpEntity = httpResponse.getEntity();
result = Integer.parseInt(EntityUtils.toString(httpEntity));
}
public int add(String name,String address,String httpPort,String tcpPort,String sId){
return result;
}
传的是 “测试”,过去后成为“æµè¯”
将参数直接传递过去,不转换为JSON 或者接收到后将JSON转换为String类型
EntityUtils.toString(httpEntity, "UTF-8") ; 这样试试,可能是编码方式不一样的原因吧。
private static String doHttpPost(URI uri, HttpClient httpClient, List<NameValuePair> nvps, String charsetEncoding) {
HttpPost httpPost = new HttpPost(uri);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charsetEncoding));
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream in = httpResponse.getEntity().getContent();
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charsetEncoding));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
sb.append(line);
}
in.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
throw ResultException.createResultException("get "+uri+"fail", e.getMessage());
} finally {
HttpClientUtils.closeQuietly(httpClient);
}
}
是这个意思不
参考以下两文
https://blog.csdn.net/qq_37918817/article/details/85232821
https://blog.csdn.net/qq_37918817/article/details/87700350