Java URL Load readURLContents(String UrlText)

Here you can find the source of readURLContents(String UrlText)

Description

read URL Contents

License

Apache License

Declaration

public static String readURLContents(String UrlText) 

Method Source Code

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

import java.net.URL;

import java.io.*;

public class Main {
    public static String readURLContents(String UrlText) {
        try {/*from   ww w  . j a v  a  2 s.c  om*/
            InputStream in = openURLStream(UrlText);
            String page = readStream(in);
            return (page);
        } catch (IOException ex) {
            throw new IllegalArgumentException(ex.toString()); // should not happen
        }
    }

    public static InputStream openURLStream(String in) throws IOException {
        URL inUrl = new URL(in);
        InputStream ret = inUrl.openStream();
        return (ret);
    }

    public static String readStream(InputStream in) {
        BufferedInputStream TheStream = null;
        StringBuffer s = new StringBuffer(2048);
        try {
            TheStream = new BufferedInputStream(in, 2048);
            int c = TheStream.read();
            while (c != -1) {
                s.append((char) c);
                c = TheStream.read();
                // ought to look at non-printing chars
            }
            TheStream.close();
        } catch (IOException e) {
            return (null);
        }
        return (s.toString());
    }
}

Related

  1. readUrl(URL url)
  2. readURL(URL url)
  3. readURL(URL url)
  4. readURLasString(URL url)
  5. readUrlContent(String address)
  6. readURLJSONArray(String urlString)
  7. readUrlProperties(URL url)
  8. readUrlStream(String urlString)
  9. readUrlText(final URL url, final String encoding)