i just wanted to ask about sending gzip for post requests using HttpClient in Android?
where to get that OutputStr开发者_如何学Goeam to be passed in the GZIPOutputstream?
any snippets?
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);
}
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);
精彩评论