HttpUrlConnection with GZIP encoding Tag(s): Networking
By default, when you make a request to URL, the response is not compressed.
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnect {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.rgagnon.com/howto.html");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// con.setRequestProperty("Accept-Encoding", "gzip");
System.out.println("Length : " + con.getContentLength());
Reader reader = new InputStreamReader(con.getInputStream());
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
Length : 21740
<!DOCTYPE HTML>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK REL="SHORTCUT ICON" HREF="http://www.rgagnon.com/favicon.ico">
<META NAME="description"
Content="Real's JAVA JAVASCRIPT WSH and PowerBuilder How-to pages with useful code snippets">
<META NAME="keywords"
Content="java,javascript,wsh,vbscript,how-to,powerbuilder">
...
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnect {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.rgagnon.com/howto.html");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept-Encoding", "gzip");
System.out.println("Length : " + con.getContentLength());
Reader reader = new InputStreamReader(con.getInputStream());
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
Length : 4865 j¼Eðßôš[è^úðGR¾vø ...
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
public class HttpConnect {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.rgagnon.com/howto.html");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept-Encoding", "gzip");
System.out.println("Length : " + con.getContentLength());
Reader reader = null;
if ("gzip".equals(con.getContentEncoding())) {
reader = new InputStreamReader(new GZIPInputStream(con.getInputStream()));
}
else {
reader = new InputStreamReader(con.getInputStream());
}
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
Length : 4865
<!DOCTYPE HTML>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK REL="SHORTCUT ICON" HREF="http://www.rgagnon.com/favicon.ico">
<META NAME="description"
Content="Real's JAVA JAVASCRIPT WSH and PowerBuilder How-to pages with useful code snippets">
<META NAME="keywords"
Content="java,javascript,wsh,vbscript,how-to,powerbuilder">
...
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com