Java URL Read getTextFromURL(URL url)

Here you can find the source of getTextFromURL(URL url)

Description

Reads the content of a URL as text using the default connect and read timeouts.

License

LGPL

Parameter

Parameter Description
url The url to get the text from

Return

a string representing the text read from the passed URL

Declaration

public static String getTextFromURL(URL url) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /** Text line separator */
    public static final String EOL = System.getProperty("line.separator", "\n");
    /** The system property to retrieve the default client connect timeout in ms.  */
    public static final String DEFAULT_CONNECT_TO = "sun.net.client.defaultConnectTimeout";
    /** The system property to retrieve the default client read timeout in ms.  */
    public static final String DEFAULT_READ_TO = "sun.net.client.defaultReadTimeout";

    /**//from   w w  w . j  a v  a 2  s. c o m
     * Reads the content of a URL as text using the default connect and read timeouts.
     * @param url The url to get the text from
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url) {
        return getTextFromURL(url, defaultConnectTimeout(), defaultReadTimeout());
    }

    /**
     * Reads the content of a URL as text
     * @param url The url to get the text from
     * @param timeout The connect and read timeout in ms.
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url, int timeout) {
        return getTextFromURL(url, timeout, timeout);
    }

    /**
     * Reads the content of a URL as text
     * @param url The url to get the text from
     * @param cTimeout The connect timeout in ms.
     * @param rTimeout The read timeout in ms.
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url, int cTimeout, int rTimeout) {
        StringBuilder b = new StringBuilder();
        InputStreamReader isr = null;
        BufferedReader br = null;
        InputStream is = null;
        URLConnection connection = null;
        try {
            connection = url.openConnection();
            connection.setConnectTimeout(cTimeout);
            connection.setReadTimeout(rTimeout);
            connection.connect();
            is = connection.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                b.append(line).append(EOL);
            }
            return b.toString();
        } catch (Exception e) {
            throw new RuntimeException("Failed to read source of [" + url + "]", e);
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (Exception e) {
                    /* No Op */}
            if (isr != null)
                try {
                    isr.close();
                } catch (Exception e) {
                    /* No Op */}
            if (is != null)
                try {
                    is.close();
                } catch (Exception e) {
                    /* No Op */}
        }
    }

    /**
     * Returns the default URL connect timeout in ms,
     * @return the connect timeout in ms,
     */
    protected static int defaultConnectTimeout() {
        return Integer.parseInt(System.getProperty(DEFAULT_CONNECT_TO, "0"));
    }

    /**
     * Returns the default URL read timeout in ms,
     * @return the read timeout in ms,
     */
    protected static int defaultReadTimeout() {
        return Integer.parseInt(System.getProperty(DEFAULT_READ_TO, "0"));
    }
}

Related

  1. getText(String url)
  2. getText(String url)
  3. getText(URL url)
  4. getTextFromURL(final String url)
  5. getTextFromURL(URL url)
  6. getURL(final URL url)
  7. getURL(String url)
  8. getURL(URL url, String params)
  9. getUrlContent(String url)