I'm trying to send post request from my android client to tomcat server containing a string of size 1MB+. I enabled GZIP in server.xml and Iv'e tried several method to compress that string from the client, but it seems that it doesn't change it size (I used DDMS and also wireshark to monitor the size of the string).
Post request (I changed the stringEntity to ByteArrayEntity for the the compress and decompress) :
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "");
httpPost.setHeader("Content-Encoding", "gzip");
StringWriter out = new StringWriter();
out.write(data);
sMsg = out.toString();
byte[] sMsgByte = compress(sMsg);
be = new ByteArrayEntity(sMsgByte);
httpPost.setEntity(be);
//se = new StringEntity(sMsg);
//httpPost.setEntity(se);
HttpResponse execute = httpClient.execute(httpPost);
Compression / decompression:
public static byte[] compress(String str) throws Exception {
if (str == null || str.length() == 0) {
return str.getBytes();
}
ByteArrayOutputStream obj=new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(obj);
gzip.write(str.getBytes("UTF-8"));
gzip.close();
return obj.toByteArray();
}
public static byte[] compress2(String str) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(str.getBytes("UTF-8"));
} finally {
if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
}
return baos.toByteArray();
}
public static String decompress(byte[] bytes) throws Exception {
if (bytes == null || bytes.length == 0) {
return bytes.toString();
}
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String outStr = "";
String line;
while ((line=bf.readLine())!=null) {
outStr += line;
}
return outStr;
}
What am I doing wrong?