首页 > 代码库 > HttpPost 传参数中文乱码

HttpPost 传参数中文乱码

 1 client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000000); 2         client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000000); 3         HttpPost post = new HttpPost(url); 4         NameValuePair rq = new BasicNameValuePair("rq", requestObj.toString()); 5         BasicNameValuePair data_type = new BasicNameValuePair("type", req_type); 6         NameValuePair sid = new BasicNameValuePair("sid", "web123"); 7         List<NameValuePair> list = new ArrayList<NameValuePair>(); 8         list.add(rq); 9         list.add(sid);10         list.add(data_type);11         StringEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8);12         post.setEntity(entity);13         HttpResponse res = client.execute(post);14         String respStr = EntityUtils.toString(res.getEntity(),HTTP.UTF_8);15         this.inputStream = new ByteArrayInputStream(respStr.toString().getBytes("utf-8"));16         return SUCCESS;


在服务端接收到requestObj.toString()中文乱码 可用在创建Entity时指定编码  StringEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8);

在返回的数据中也出现了中文乱码 可使用EntityUtils.toString指定字符编码     String respStr = EntityUtils.toString(res.getEntity(),HTTP.UTF_8);

HttpPost 传参数中文乱码