While using httpClient to retrieve content from a HTTP response, I got a content that I can not recognized and it looked like a bunch of compressed data. I figured that it might be due to the content encoding and the following is a code snippet that I used to solve this issue:
private String gzipIS2String(InputStream is) {
String result = "";
String line="";
try {
GZIPInputStream gzipIS = new GZIPInputStream(is);
Reader decoder = new InputStreamReader (gzipIS, "UTF-8");
BufferedReader buffered = new BufferedReader (decoder);
while ((line = buffered.readLine()) != null) {
result += line;
}
}
catch(IOException e) {
e.printStackTrace();
}
return result;
}
public String getURL(String url) {
String result = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "iTunes-AppleTV/5.1 (3; 8GB; dt:12)");
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity.getContentEncoding().toString().contains("gzip")) {
result = gzipIS2String(httpEntity.getContent());
}
else {
result = EntityUtils.toString(httpEntity);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
Reference links
http://stackoverflow.com/questions/13866253/gzip-decompressing-returns-random-characters
http://stackoverflow.com/questions/2474193/uncompress-gziped-http-response-in-java
http://stackoverflow.com/questions/15040504/how-to-converting-bufferedreader-to-string-at-once
http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html
沒有留言:
張貼留言