Example usage for java.net HttpURLConnection getContent

List of usage examples for java.net HttpURLConnection getContent

Introduction

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

Prototype

public Object getContent() throws IOException 

Source Link

Document

Retrieves the contents of this URL connection.

Usage

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

public static String getSourceCode(URL url) {
    Object content = null;//  w  w w  .  jav a2  s  .co  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:org.elasticsearch.discovery.custom.DataFetcher.java

public static String fetchData(String url, ESLogger logger) {
    DataInputStream responseStream = null;
    try {/*from w  ww  .jav  a  2  s .c om*/
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() != 200)
            throw new RuntimeException("Unable to get data for URL " + url);

        byte[] b = new byte[2048];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        responseStream = new DataInputStream((FilterInputStream) conn.getContent());
        int c = 0;
        while ((c = responseStream.read(b, 0, b.length)) != -1)
            bos.write(b, 0, c);
        String return_ = new String(bos.toByteArray(), CharEncoding.UTF_8);
        logger.info(String.format("Calling URL API: %s returns: %s", url, return_));
        conn.disconnect();
        return return_;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (responseStream != null)
                responseStream.close();
        } catch (Exception e) {
            logger.warn("Failed to close response stream from priam", e);
        }
    }
}

From source file:org.openmrs.web.filter.initialization.TestInstallUtil.java

/**
 * Tests the connection to the specified URL
 *
 * @param urlString the url to test/*from  w  w  w.j  ava2s .  c o  m*/
 * @return true if a connection a established otherwise false
 */
protected static boolean testConnection(String urlString) {
    try {
        HttpURLConnection urlConnect = (HttpURLConnection) new URL(urlString).openConnection();
        //wait for 15sec
        urlConnect.setConnectTimeout(15000);
        urlConnect.setUseCaches(false);
        //trying to retrieve data from the source. If there
        //is no connection, this line will fail
        urlConnect.getContent();
        return true;
    } catch (UnknownHostException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error generated:", e);
        }
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error generated:", e);
        }
    }

    return false;
}

From source file:com.netflix.raigad.utils.SystemUtils.java

public static String getDataFromUrl(String url) {
    try {/*from  w ww.  jav a2  s .c o m*/
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(1000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Unable to get data for URL " + url);
        }
        byte[] b = new byte[2048];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataInputStream d = new DataInputStream((FilterInputStream) conn.getContent());
        int c = 0;
        while ((c = d.read(b, 0, b.length)) != -1)
            bos.write(b, 0, c);
        String return_ = new String(bos.toByteArray(), Charsets.UTF_8);
        logger.info("Calling URL API: {} returns: {}", url, return_);
        conn.disconnect();
        return return_;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:com.gisgraphy.importer.ImporterHelper.java

/**
 * check if an url doesn't return 200 or 3XX code
 * @param urlAsString the url to check//from w ww  . j  a  v a 2 s  . c o m
 * @return true if the url exists and is valid
 */
public static boolean checkUrl(String urlAsString) {
    if (urlAsString == null) {
        logger.error("can not check null URL");
        return false;
    }
    URL url;
    try {
        url = new URL(urlAsString);
    } catch (MalformedURLException e) {
        logger.error(urlAsString + " is not a valid url, can not check.");
        return false;
    }
    int responseCode;
    String responseMessage = "NO RESPONSE MESSAGE";
    Object content = "NO CONTENT";
    HttpURLConnection huc;
    try {
        huc = (HttpURLConnection) url.openConnection();
        huc.setRequestMethod("HEAD");
        responseCode = huc.getResponseCode();
        content = huc.getContent();
        responseMessage = huc.getResponseMessage();
    } catch (ProtocolException e) {
        logger.error("can not check url " + e.getMessage(), e);
        return false;
    } catch (IOException e) {
        logger.error("can not check url " + e.getMessage(), e);
        return false;
    }

    if (responseCode == 200 || (responseCode > 300 && responseCode < 400)) {
        logger.info("URL " + urlAsString + " exists");
        return true;
    } else {
        logger.error(urlAsString + " return a " + responseCode + " : " + content + "/" + responseMessage);
        return false;
    }
}

From source file:net.ftb.util.DownloadUtils.java

/**
 * @param repoURL - URL on the repo//  w w w.  j  a v  a  2s  . com
 * @param fullDebug - should this dump the full cloudflare debug info in the console
 * @return boolean representing if the file exists
 */
public static boolean CloudFlareInspector(String repoURL, boolean fullDebug) {
    try {
        boolean ret;
        HttpURLConnection connection = (HttpURLConnection) new URL(repoURL + "cdn-cgi/trace").openConnection();
        if (!fullDebug)
            connection.setRequestMethod("HEAD");
        Logger.logInfo("CF-RAY: " + connection.getHeaderField("CF-RAY"));
        if (fullDebug)
            Logger.logInfo("CF Debug Info: " + connection.getContent().toString());
        ret = connection.getResponseCode() == 200;
        IOUtils.close(connection);
        return ret;
    } catch (Exception e) {
        return false;
    }
}

From source file:WebReader.java

void getData(String address) throws Exception {
    setTitle(address);/*from  w ww  . j a  v a2s  . c om*/
    URL page = new URL(address);
    StringBuffer text = new StringBuffer();
    HttpURLConnection conn = (HttpURLConnection) page.openConnection();
    conn.connect();
    InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
    BufferedReader buff = new BufferedReader(in);
    box.setText("Getting data ...");
    String line;
    do {
        line = buff.readLine();
        text.append(line + "\n");
    } while (line != null);
    box.setText(text.toString());
}

From source file:org.sonar.batch.RemoteServerMetadata.java

protected String remoteContent(String path) throws IOException {
    String fullUrl = getUrlFor(path);
    HttpURLConnection conn = getConnection(fullUrl, "GET");
    InputStream input = (InputStream) conn.getContent();
    try {/* w  w  w  . j  a v  a2 s .  c o  m*/
        int statusCode = conn.getResponseCode();
        if (statusCode != HttpURLConnection.HTTP_OK) {
            throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode);
        }
        return IOUtil.toString(input);

    } finally {
        IOUtil.close(input);
        conn.disconnect();
    }
}

From source file:edu.rit.chrisbitler.ritcraft.slackintegration.rtm.RTMClient.java

/**
 * Send a slack message to a channel via the web api
 * @param text The text to send to the channel
 * @param channel The channel ID to send it to (usually the relay channel)
 *///from ww w.j a v a2  s .  c  om
public void sendSlackMessage(String text, String channel) {
    try {
        URL chat = new URL("https://slack.com/api/chat.postMessage?token=" + SlackIntegration.BOT_TOKEN
                + "&channel=" + channel + "&text=" + URLEncoder.encode(text, "UTF-8"));
        HttpURLConnection conn = (HttpURLConnection) chat.openConnection();
        conn.connect();
        conn.getContent();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.codehaus.mojo.sonar.ServerMetadata.java

protected String remoteContent(String path) throws IOException {
    String fullUrl = url + path;/* w ww  . ja  v  a2  s  . co m*/
    HttpURLConnection conn = getConnection(fullUrl + path, "GET");
    Reader reader = new InputStreamReader((InputStream) conn.getContent());
    try {
        int statusCode = conn.getResponseCode();
        if (statusCode != HttpURLConnection.HTTP_OK) {
            throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode);
        }
        return IOUtils.toString(reader);

    } finally {
        IOUtils.closeQuietly(reader);
        conn.disconnect();
    }
}