Example usage for java.net URLConnection getContentEncoding

List of usage examples for java.net URLConnection getContentEncoding

Introduction

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

Prototype

public String getContentEncoding() 

Source Link

Document

Returns the value of the content-encoding header field.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    try {//w  ww  .  ja v a 2  s . c  om
        URL u = new URL("http://www.java2s.com");
        URLConnection uc = u.openConnection();
        System.out.println("Content-encoding: " + uc.getContentEncoding());
    } catch (MalformedURLException e) {
        System.err.println("not a URL I understand");
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.connect();/*from w  w w.j  a v a 2s. c o  m*/
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
}

From source file:URLConnectionTest.java

public static void main(String[] args) {
    try {/*from ww  w  . j a  va  2s . c o  m*/
        String urlName;
        if (args.length > 0)
            urlName = args[0];
        else
            urlName = "http://java.sun.com";

        URL url = new URL(urlName);
        URLConnection connection = url.openConnection();

        // set username, password if specified on command line

        if (args.length > 2) {
            String username = args[1];
            String password = args[2];
            String input = username + ":" + password;
            String encoding = base64Encode(input);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
        }

        connection.connect();

        // print header fields

        Map<String, List<String>> headers = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String key = entry.getKey();
            for (String value : entry.getValue())
                System.out.println(key + ": " + value);
        }

        // print convenience functions

        System.out.println("----------");
        System.out.println("getContentType: " + connection.getContentType());
        System.out.println("getContentLength: " + connection.getContentLength());
        System.out.println("getContentEncoding: " + connection.getContentEncoding());
        System.out.println("getDate: " + connection.getDate());
        System.out.println("getExpiration: " + connection.getExpiration());
        System.out.println("getLastModifed: " + connection.getLastModified());
        System.out.println("----------");

        Scanner in = new Scanner(connection.getInputStream());

        // print first ten lines of contents

        for (int n = 1; in.hasNextLine() && n <= 10; n++)
            System.out.println(in.nextLine());
        if (in.hasNextLine())
            System.out.println(". . .");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static InputStream getInputEncoding(URLConnection connection) throws IOException {
    InputStream in;/*  ww  w .j  a va 2s . c  om*/
    String encoding = connection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(connection.getInputStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        in = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
    } else {
        in = connection.getInputStream();
    }
    return in;
}

From source file:org.enderstone.server.uuid.ServerRequest.java

public static JSONObject parseDataFromURL(String connect) throws IOException {
    URL url = new URL(connect);
    URLConnection uc = url.openConnection();
    uc.setConnectTimeout(5000);//from   w w  w.j  a  v a 2 s.  co  m
    uc.setReadTimeout(5000);
    uc.connect();
    String encoding = uc.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    try (Scanner scanner = new Scanner(uc.getInputStream(), encoding)) {
        scanner.useDelimiter("\\A");
        return new JSONObject(scanner.next());
    } catch (NoSuchElementException noskin) {
        return new JSONObject();
    }
}

From source file:be.geecko.openlauncher.net.SuggestionsTask.java

private static String readUrl(String http) throws IOException {
    URL url = new URL(http);
    URLConnection connection = url.openConnection();
    InputStream in = connection.getInputStream();
    String encoding = connection.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    return IOUtils.toString(in, encoding);
}

From source file:org.parakoopa.gmnet.tests.MasterServerHelper.java

public static String getPublicIp() throws IOException {
    //URLConnection con = new URL("http://icanhazip.com/").openConnection();
    URLConnection con = new URL("http://95.85.63.183/gmnet/test-api/ip.php").openConnection();
    InputStream in = con.getInputStream();
    String encoding = con.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    return IOUtils.toString(in, encoding).trim();
}

From source file:com.google.android.feeds.ContentHandlerUtils.java

/**
 * Returns the uncompressed {@link InputStream} for the given
 * {@link URLConnection}.//from   www .  j a  v  a 2s  .  c  o m
 */
public static InputStream getUncompressedInputStream(URLConnection connection) throws IOException {
    InputStream source = connection.getInputStream();
    String encoding = connection.getContentEncoding();
    if ("gzip".equalsIgnoreCase(encoding)) {
        return new GZIPInputStream(source);
    } else if ("deflate".equalsIgnoreCase(encoding)) {
        boolean noHeader = true;
        Inflater inflater = new Inflater(noHeader);
        return new InflaterInputStream(source, inflater);
    } else {
        return source;
    }
}

From source file:nz.co.lolnet.james137137.MemoryChecker.java

public static String downloadTextFromUrl(URL url)
        throws MalformedURLException, IOException, UnknownHostException {
    URLConnection con = url.openConnection();
    InputStream in = con.getInputStream();
    String encoding = con.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    String body = IOUtils.toString(in, encoding);
    return body;/*w w w. j  av  a2  s  .c  om*/
}