}
//////////////////////
static public String getUrlContentsAsString(HTTPSession session, String urlString, int maxKbytes) {
HTTPSession useSession = session;
try {
if (useSession == null)
useSession = new HTTPSession();
HTTPMethod m = useSession.newMethodGet(urlString);
m.setFollowRedirects(true);
m.setRequestHeader("Accept-Encoding", "gzip,deflate");
int status = m.execute();
if (status != 200) {
throw new RuntimeException("failed status = " + status);
}
String charset = m.getResponseCharSet();
if (charset == null) charset = "UTF-8";
// check for deflate and gzip compression
Header h = m.getResponseHeader("content-encoding");
String encoding = (h == null) ? null : h.getValue();
if (encoding != null && encoding.equals("deflate")) {
byte[] body = m.getResponseAsBytes();
InputStream is = new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(body)), 10000);
return readContents(is, charset, maxKbytes);
} else if (encoding != null && encoding.equals("gzip")) {
byte[] body = m.getResponseAsBytes();
InputStream is = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(body)), 10000);
return readContents(is, charset, maxKbytes);
} else {
byte[] body = m.getResponseAsBytes(maxKbytes * 1000);
return new String(body, charset);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if ((session == null) && (useSession != null))
useSession.close();
}
}