3

i just wanted to ask about sending gzip for post requests using HttpClient in Android?

where to get that OutputStream to be passed in the GZIPOutputstream?

any snippets?

Mikey
  • 4,218
  • 3
  • 25
  • 25

2 Answers2

6

Hi UseHttpUriRequest as shown below

 String urlval=" http"//www.sampleurl.com/";
    HttpUriRequest req = new HttpGet(urlval);
    req.addHeader("Accept-Encoding", "gzip");
    httpClient.execute(req);

and then Check response for content encoding as shown below :

InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    is = new GZIPInputStream(is);
}
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • 2
    the person asked about sending gzip'd requests. the above tells the server that you can accept gzip'd responses and handles them when you get them, but it does not gzip the outgoing content. – Jeffrey Blattman May 04 '11 at 17:53
  • true, but useful too to test if we are receiving gzip content. – neobie Aug 17 '12 at 04:03
2

If your data is not too large, you can do it like this:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(POST_URL);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data.getBytes());
gos.close();
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(baos.toByteArray());
httpost.setEntity(byteArrayEntity);
akjoshi
  • 15,374
  • 13
  • 103
  • 121
justin
  • 21
  • 3