Example usage for java.net HttpURLConnection getInputStream

List of usage examples for java.net HttpURLConnection getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.quackware.handsfreemusic.Utility.java

public static String getSourceCode(URL url) {
    Object content = null;/*from ww  w  .jav  a 2 s  . c  o  m*/
    try {

        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4");
        uc.connect();
        InputStream stream = uc.getInputStream();
        if (stream != null) {
            content = readStream(uc.getContentLength(), stream);
        } else if ((content = uc.getContent()) != null && content instanceof java.io.InputStream)
            content = readStream(uc.getContentLength(), (java.io.InputStream) content);
        uc.disconnect();

    } catch (Exception ex) {
        return null;
    }
    if (content != null && content instanceof String) {
        String html = (String) content;
        return html;
    } else {
        return null;
    }
}

From source file:com.kubeiwu.commontool.khttp.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * //ww w. j  av a 2 s .c  o  m
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {

    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    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.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Leer contenido texto UTF-8 desde el URL dado
 *
 * @param _url el URL a leer/*from   w  ww  .j a  v  a2 s.co  m*/
 * @return el contenido del URL como String
 * @throws Exception
 */
public static String read(String _url) throws Exception {

    StringBuilder b = new StringBuilder();
    try {

        //logger.info("HTTP request: " + _url);
        final URL url = new URL(_url);
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
        b.append(slurp(connection.getInputStream(), 1024));

        connection.disconnect();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return b.toString();

}

From source file:Main.java

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

    try {/*from   ww w  .j a va  2  s  .c  om*/
        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:Main.java

private static String getStringFromUrl(String url, int connectTimeout, int readTimeout)
        throws MalformedURLException, JSONException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
    String jsonString = "";

    if (connectTimeout != 0) {
        urlConn.setConnectTimeout(connectTimeout);
    }/*from   w w  w . j  av  a  2 s. com*/
    if (readTimeout != 0) {
        urlConn.setReadTimeout(readTimeout);
    }

    try {
        jsonString = getStringFromInputStream(urlConn.getInputStream());
    } finally {
        urlConn.disconnect();
    }
    return jsonString;
}

From source file:de.cubeisland.engine.core.util.McUUID.java

public static NameEntry[] getNameHistory(UUID playerUUID) {
    try {//www .j  av a 2 s .c o m
        HttpURLConnection connection = (HttpURLConnection) new URL(
                String.format(MOJANG_API_URL_UUID_NAMEHISTORY, playerUUID.toString().replace("-", "")))
                        .openConnection();
        return mapper.readValue(connection.getInputStream(), NameEntry[].class);
    } catch (MalformedURLException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        CubeEngine.getLog().error(e, "Could not retrieve NameHistory for given UUID!");
        return emptyHistory;
    }
}

From source file:io.github.retz.scheduler.MesosHTTPFetcher.java

private static Optional<String> fetchSlaveAddr(String master, String slaveId) {
    URL url;//from w  ww . j  a va  2s.c  o m
    try {
        url = new URL("http://" + master + "/slaves");
    } catch (MalformedURLException e) {
        return Optional.empty();
    }
    HttpURLConnection conn;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        return extractSlaveAddr(conn.getInputStream(), slaveId);
    } catch (IOException e) {
        return Optional.empty();
    }
}

From source file:com.android.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection//from   ww w  .jav  a2s.c om
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    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.wx.kernel.util.HttpKit.java

private static String readResponseString(HttpURLConnection conn) {
    StringBuilder sb = new StringBuilder();
    InputStream inputStream = null;
    try {/*ww  w.jav  a2  s . c om*/
        inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, CHARSET));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection//  w w w  . j  a v  a2s.c  o  m
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    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;
}