Code example for URLConnection

Methods: connect, getContentEncoding, getInputStream, setRequestProperty

        URLConnection conn = url.openConnection();
        //If it is the http or https scheme, then ask for gzip if the server supports it. 
        if (conn instanceof HttpURLConnection) {
            //Send the requested encoding to the remote server. 
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        //Conect to get the response headers 
        //Return the correct inputstream 
        //If the connection is a url, connection, check the response encoding. 
        if (conn instanceof HttpURLConnection) {
            //Get the content encoding of the server response 
            String encoding = conn.getContentEncoding();
            //If null, set it to a emtpy string 
            if (encoding == null) {
                encoding = "";
            if (encoding.equalsIgnoreCase("gzip")) {
                //For gzip input stream, use a GZIPInputStream 
                return new GZIPInputStream(conn.getInputStream());
            } else if (encoding.equalsIgnoreCase("deflate")) {
                //If it is encoded as deflate, then select the inflater inputstream. 
                return new InflaterInputStream(conn.getInputStream(), new Inflater(true));
            } else { 
                //Else read the raw bytes 
                return conn.getInputStream();
        } else { 
            //Else read the raw bytes. 
            return conn.getInputStream();
     * Parses an 'option-holding' parameters in the following form