Java HTTP Get readURL(final String textURL)

Here you can find the source of readURL(final String textURL)

Description

Read string from URL.

License

BSD License

Declaration

public static String readURL(final String textURL) 

Method Source Code

//package com.java2s;
/**//from   w w  w.j  a v a 2  s  .  c o  m
 * Copyright (C) 2011-2013 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**
     * Read string from URL.
     */
    public static String readURL(final String textURL) {

        final StringBuilder text = new StringBuilder(128);

        try {

            final URL url = new URL(textURL);

            final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setConnectTimeout(2 * 1000);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", "config-reader");
            connection.connect();

            final InputStream input = connection.getInputStream();
            final InputStreamReader reader = new InputStreamReader(input);
            final BufferedReader buffered = new BufferedReader(reader);

            String line;
            while ((line = buffered.readLine()) != null) {
                text.append(line);
            }

            buffered.close();

        } catch (final Exception e) {
            text.append(e.getMessage());
        }

        return text.toString();

    }
}

Related

  1. httpGet(String urlStr)
  2. httpGet(String urlStr)
  3. httpGet(String urlToRead)
  4. httpGetString(String url)
  5. readUrl(final String strUrl)
  6. readUrl(HttpURLConnection conn)
  7. readUrl(String url, String token)
  8. readURL(String url, String type)
  9. readUrl(String urlAsString, int timeout)