Example usage for java.net HttpURLConnection getContentEncoding

List of usage examples for java.net HttpURLConnection getContentEncoding

Introduction

In this page you can find the example usage for java.net HttpURLConnection getContentEncoding.

Prototype

public String getContentEncoding() 

Source Link

Document

Returns the value of the content-encoding header field.

Usage

From source file:com.google.cloud.runtime.jetty.util.HttpUrlUtil.java

/**
 * Obtain the text (non-binary) response body from an {@link HttpURLConnection},
 * using the response provided charset.//from  ww  w  .  ja  va2 s. c  om
 * <p>
 * Note: Normal HttpURLConnection doesn't use the provided charset properly.
 * </p>
 *
 * @param http the {@link HttpURLConnection} to obtain the response body from
 * @return the text of the response body
 * @throws IOException if unable to get the text of the response body
 */
public static String getResponseBody(HttpURLConnection http) throws IOException {
    Charset responseEncoding = StandardCharsets.UTF_8;
    if (http.getContentEncoding() != null) {
        responseEncoding = Charset.forName(http.getContentEncoding());
    }

    return IOUtils.toString(http.getInputStream(), responseEncoding);
}

From source file:Main.java

static String downloadHtml(String urlString) {
    StringBuffer buffer = new StringBuffer();

    try {/*from   w  w w. ja v  a2 s .com*/
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        HttpURLConnection.setFollowRedirects(true);
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        String encoding = conn.getContentEncoding();
        InputStream inStr = null;

        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            inStr = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        } else {
            inStr = conn.getInputStream();
        }
        int ptr = 0;
        InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312"));

        while ((ptr = inStrReader.read()) != -1) {
            buffer.append((char) ptr);
        }
        inStrReader.close();
        conn.disconnect();
        inStr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buffer.toString();
}

From source file:org.droidparts.http.worker.HTTPInputStream.java

public static HTTPInputStream getInstance(HttpURLConnection conn, boolean useErrorStream) throws HTTPException {
    try {/*from w  w w .j a  va 2 s . c  om*/
        InputStream is = useErrorStream ? conn.getErrorStream() : conn.getInputStream();
        is = getUnpackedInputStream(conn.getContentEncoding(), is);
        return new HTTPInputStream(is, conn, null);
    } catch (Exception e) {
        throw new HTTPException(e);
    }
}

From source file:org.geotools.wfs.protocol.DefaultConnectionFactory.java

/**
 * If the connection content-encoding contains the {@code gzip} flag creates
 * a gzip inputstream, otherwise returns a normal buffered input stream by
 * opening the http connection.// w ww . ja v  a  2  s  . co m
 *
 * @param hc
 *            the connection to use to create the stream
 * @return an input steam from the provided connection
 */
private static InputStream getInputStream(final HttpURLConnection hc, final boolean tryGZIP)
        throws IOException {
    InputStream is = hc.getInputStream();

    if (tryGZIP) {
        if (hc.getContentEncoding() != null && hc.getContentEncoding().indexOf("gzip") != -1) {
            is = new GZIPInputStream(is);
        }
    }
    is = new BufferedInputStream(is);
    // special logger for communication information only.
    Logger logger = Logging.getLogger("org.geotools.data.communication");
    return is;
}

From source file:org.jumpmind.symmetric.transport.http.HttpTransportManager.java

protected static InputStream getInputStreamFrom(HttpURLConnection connection) throws IOException {
    String type = connection.getContentEncoding();
    InputStream in = connection.getInputStream();
    if (!StringUtils.isBlank(type) && type.equals("gzip")) {
        in = new GZIPInputStream(in);
    }/* www .  j a  v  a2  s.  c o  m*/
    return in;
}

From source file:org.jumpmind.symmetric.transport.http.HttpTransportManager.java

/**
 * If the content is gzip'd, then uncompress.
 *///from  w w w  .  ja v  a 2s  .com
protected static BufferedReader getReaderFrom(HttpURLConnection connection) throws IOException {
    String type = connection.getContentEncoding();
    InputStream in = connection.getInputStream();
    if (!StringUtils.isBlank(type) && type.equals("gzip")) {
        in = new GZIPInputStream(in);
    }
    return TransportUtils.toReader(in);
}

From source file:common.net.volley.toolbox.HurlStack.java

@Nullable
private static InputStream applyDecompressionIfApplicable(HttpURLConnection conn, @Nullable InputStream in)
        throws IOException {
    if (in != null && GZIP_ENCODING.equals(conn.getContentEncoding())) {
        return new GZIPInputStream(in);
    }/*from w  w w. j  a va  2  s .co m*/
    return in;
}

From source file:de.jetwick.util.Translate.java

public static String download(String urlAsString) {
    try {//from   w  w w  .j a v  a2s.  com
        URL url = new URL(urlAsString);
        //using proxy may increase latency
        HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        hConn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
        hConn.addRequestProperty("Referer", "http://jetsli.de/crawler");

        hConn.setConnectTimeout(2000);
        hConn.setReadTimeout(2000);
        InputStream is = hConn.getInputStream();
        if ("gzip".equals(hConn.getContentEncoding()))
            is = new GZIPInputStream(is);

        return getInputStream(is);
    } catch (Exception ex) {
        return "";
    }
}

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private static HttpEntity getEntity(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;/*from w  w  w.j ava  2 s  .  c o  m*/
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

public static void download(String urlFrom, String login, String pwd, OutputStream destStream)
        throws Exception {
    try {/*from  w w w . j a  v a  2 s .  co  m*/
        HttpURLConnection uc = prepareConnection(urlFrom, login, pwd);

        uc.connect();

        if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}",
                    new Object[] { uc.getResponseCode(), uc.getResponseMessage() });

            return;
        }

        BufferedInputStream is = null;

        if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip"))
                || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) {
            is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream()));

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}",
                    urlFrom);
        } else {
            is = new BufferedInputStream(uc.getInputStream());

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO,
                    "Will download not compressed data from:{0}", urlFrom);
        }

        StreamsHelper.copy(is, destStream);
        destStream.flush();

    } catch (Exception ex) {
        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex);

        throw ex;
    }
}