Skip to content

Commit

Permalink
handle gzip on getContent
Browse files Browse the repository at this point in the history
  • Loading branch information
bvolpato committed Feb 7, 2018
1 parent 09c31a2 commit 10667b0
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main/java/org/brunocvcunha/inutils4j/MyHTTPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ public static InputStream getContentStream(String stringUrl) throws MalformedURL

URLConnection urlConnection = url.openConnection();


InputStream stream;
if (urlConnection.getContentEncoding() != null && urlConnection.getContentEncoding().equals("gzip")) {
stream = new GZIPInputStream(urlConnection.getInputStream());
} else {
stream = urlConnection.getInputStream();
InputStream is = urlConnection.getInputStream();
if ("gzip".equals(urlConnection.getContentEncoding())) {
is = new GZIPInputStream(is);
}
return stream;

return is;
}

/**
Expand Down Expand Up @@ -115,7 +113,12 @@ public static String getContent(String stringUrl, Map<String, String> parameters
}
}

return MyStreamUtils.readContent(conn.getInputStream());
InputStream is = conn.getInputStream();
if ("gzip".equals(conn.getContentEncoding())) {
is = new GZIPInputStream(is);
}

return MyStreamUtils.readContent(is);
}


Expand Down Expand Up @@ -197,8 +200,13 @@ public static Map<String, List<String>> getResponseHeaders(String stringUrl,
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(followRedirects);

InputStream is = conn.getInputStream();
if ("gzip".equals(conn.getContentEncoding())) {
is = new GZIPInputStream(is);
}

Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>(conn.getHeaderFields());
headers.put("X-Content", Arrays.asList(MyStreamUtils.readContent(conn.getInputStream())));
headers.put("X-Content", Arrays.asList(MyStreamUtils.readContent(is)));
headers.put("X-URL", Arrays.asList(conn.getURL().toString()));
headers.put("X-Status", Arrays.asList(String.valueOf(conn.getResponseCode())));

Expand Down

0 comments on commit 10667b0

Please sign in to comment.