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:org.parakoopa.gmnet.tests.MasterServerHelper.java

public static boolean stop() {
    URLConnection con;
    try {//from  w w  w. j a  v a 2 s. c o m
        con = new URL(Workspace.MASTER_URL_STOP).openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String body = IOUtils.toString(in, encoding).trim();
        return body.equals("success.");
    } catch (IOException e) {
        logger.error("[MasterServerHelper] Error while trying to stop master server.", e);
        return false;
    }
}

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

public static boolean start() {
    URLConnection con;
    try {//w w w .  j av a2  s  . c om
        con = new URL(Workspace.MASTER_URL_START).openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String body = IOUtils.toString(in, encoding).trim();
        return body.equals("success.");
    } catch (IOException e) {
        logger.error("[MasterServerHelper] Error while trying to start master server.", e);
        return false;
    }
}

From source file:org.benjp.listener.ServerBootstrap.java

private static String callServer(String serviceUri, String params) {

    String serviceUrl = getServerBase() + PropertyManager.getProperty(PropertyManager.PROPERTY_CHAT_SERVER_URL)
            + "/" + serviceUri + "?passphrase="
            + PropertyManager.getProperty(PropertyManager.PROPERTY_PASSPHRASE) + "&" + params;
    String body = "";
    try {//from  ww  w .  ja va  2s .com
        URL url = new URL(serviceUrl);
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        body = IOUtils.toString(in, encoding);
        if ("null".equals(body))
            body = null;
    } catch (MalformedURLException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return body;
}

From source file:mc.lib.network.NetworkHelper.java

public static String getAsText(String url, String encoding) {
    InputStream is = null;//w  w  w .j a v a2  s .c  o m
    try {
        URLConnection c = new URL(url).openConnection();
        c.connect();
        is = c.getInputStream();
        if (c.getContentEncoding() != null)
            encoding = c.getContentEncoding();
        return StreamHelper.readNetworkStream(is, encoding, c.getReadTimeout());
    } catch (MalformedURLException e) {
        Log.e(LOGTAG, "Wrong url format", e);
    } catch (Exception e) {
        Log.e(LOGTAG, "Cannot get text", e);
    } finally {
        StreamHelper.close(is);
    }
    return null;
}

From source file:GetURLInfo.java

/** Use the URLConnection class to get info about the URL */
public static void printinfo(URL url) throws IOException {
    URLConnection c = url.openConnection(); // Get URLConnection from URL
    c.connect(); // Open a connection to URL

    // Display some information about the URL contents
    System.out.println("  Content Type: " + c.getContentType());
    System.out.println("  Content Encoding: " + c.getContentEncoding());
    System.out.println("  Content Length: " + c.getContentLength());
    System.out.println("  Date: " + new Date(c.getDate()));
    System.out.println("  Last Modified: " + new Date(c.getLastModified()));
    System.out.println("  Expiration: " + new Date(c.getExpiration()));

    // If it is an HTTP connection, display some additional information.
    if (c instanceof HttpURLConnection) {
        HttpURLConnection h = (HttpURLConnection) c;
        System.out.println("  Request Method: " + h.getRequestMethod());
        System.out.println("  Response Message: " + h.getResponseMessage());
        System.out.println("  Response Code: " + h.getResponseCode());
    }/*  w  ww. ja  va2  s  .co m*/
}

From source file:org.exoplatform.chat.listener.ServerBootstrap.java

private static String callServer(String serviceUri, String params) {

    String serviceUrl = getServerBase() + PropertyManager.getProperty(PropertyManager.PROPERTY_CHAT_SERVER_URL)
            + "/" + serviceUri + "?passphrase="
            + PropertyManager.getProperty(PropertyManager.PROPERTY_PASSPHRASE) + "&" + params;
    String body = "";
    try {//from  w  w  w  .ja v a  2s . co m
        URL url = new URL(serviceUrl);
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        body = IOUtils.toString(in, encoding);
        if ("null".equals(body))
            body = null;
    } catch (MalformedURLException e) {
        LOG.warning(e.getMessage());
    } catch (IOException e) {
        LOG.warning(e.getMessage());
    }
    return body;
}

From source file:fr.free.movierenamer.utils.URIRequest.java

private static InputStream getInputStreamNoSync(URLConnection connection) throws IOException {
    String encoding = connection.getContentEncoding();
    InputStream inputStream;// w  ww.jav  a  2s .co  m
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        throw ioe;
    }

    if ("gzip".equalsIgnoreCase(encoding)) {
        inputStream = new GZIPInputStream(inputStream);
    } else if ("deflate".equalsIgnoreCase(encoding)) {
        inputStream = new InflaterInputStream(inputStream, new Inflater(true));
    }

    return inputStream;
}

From source file:org.kalypso.commons.java.net.UrlUtilities.java

/**
 * Find content encoding for the given connection. If the connection denotes a platform-url, we fetch the encoding
 * from the underlying resource, as the connection will not return a valid charset.
 *///  w  w  w.ja v a2 s .com
public static String findEncoding(final URLConnection connection) {
    final IFile file = ResourceUtilities.findFileFromURL(connection.getURL());
    if (file == null)
        return connection.getContentEncoding();

    try {
        return file.getCharset();
    } catch (final CoreException e) {
        e.printStackTrace();
        return connection.getContentEncoding();
    }
}

From source file:co.marcin.novaguilds.util.StringUtils.java

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

From source file:org.benjp.listener.ServerBootstrap.java

private static String postServer(String serviceUri, String params) {
    String serviceUrl = getServerBase() + PropertyManager.getProperty(PropertyManager.PROPERTY_CHAT_SERVER_URL)
            + "/" + serviceUri;
    String allParams = "passphrase=" + PropertyManager.getProperty(PropertyManager.PROPERTY_PASSPHRASE) + "&"
            + params;/*from  ww  w.j  av  a 2 s .c  o m*/
    String body = "";
    OutputStreamWriter writer = null;
    try {
        URL url = new URL(serviceUrl);
        URLConnection con = url.openConnection();
        con.setDoOutput(true);

        //envoi de la requte
        writer = new OutputStreamWriter(con.getOutputStream());
        writer.write(allParams);
        writer.flush();

        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        body = IOUtils.toString(in, encoding);
        if ("null".equals(body))
            body = null;

    } catch (MalformedURLException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

    } finally {
        try {
            writer.close();
        } catch (Exception e) {
        }
    }
    return body;
}