最近有一个需求,上传软件中用户的自定义内容。遇到一个问题,post 能传递上数据,但是上传的返回值却是乱码。
代码如下:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://xx.xx.xx.xx");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uid", "fff"));
params.add(new BasicNameValuePair("action", "set"));
params.add(new BasicNameValuePair("data", "112"));
HttpEntity entity = new UrlEncodedFormEntity(params);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
Log.i("~peter", EntityUtils.toString(responseEntity, "utf-8"));
} else {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
httpClient.getConnectionManager().shutdown();
}
打印log 如下:
02-18 15:47:11.365: I/~peter(6092): ~peter �������������+)*M���L��������
后来找到原因:
原因在于代码中的这句:
httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
在header 中 使用 Accept-Encoding字段,服务端默认使用了第一个gzip的压缩格式传输数据。所以会出现乱码。
两种解决方法:
1. 去掉这句 : httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
2. 接收消息的地方解压一下。
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mm.maxthon.cn/mxbrowser/userdata.php");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uid", "fff"));
params.add(new BasicNameValuePair("action", "set"));
params.add(new BasicNameValuePair("data", "112"));
HttpEntity entity = new UrlEncodedFormEntity(params);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
// HttpEntity responseEntity = response.getEntity();
// if (responseEntity != null) {
// Log.i("~peter", EntityUtils.toString(responseEntity, "utf-8"));
// } else {
//
// }
//解压返回数据
byte[] body = EntityUtils.toByteArray(response.getEntity());
if(MxHttpClient.isGzipContentEnc(response)){
body = MxHttpClient.unzipData(body);
}
Log.i("~peter", "result=" + new String(body));
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
httpClient.getConnectionManager().shutdown();
}
public static boolean isGzipContentEnc(HttpResponse response) {
String contentEnc = null;
Header header = response.getEntity().getContentEncoding();
if (header != null) {
contentEnc = header.getValue();
}
if (contentEnc != null && contentEnc.toLowerCase().indexOf("gzip") >= 0) {
return true;
}
return false;
}
public static final byte[] unzipData(byte[] in) {
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
try {
bais = new ByteArrayInputStream(in);
baos = new ByteArrayOutputStream();
unZipData(bais, baos);
}// of try
catch (Exception ioe) {
ioe.printStackTrace();
baos = null;
} finally {
try {
if (bais != null)
bais.close();
if (baos != null)
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return baos.toByteArray();
}