Java URL Read readUrl(final String url)

Here you can find the source of readUrl(final String url)

Description

read Url

License

Open Source License

Declaration

public static String readUrl(final String url) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

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

public class Main {
    public static String readUrl(final String url) {
        try {//from  www .j a v  a 2  s .  c om
            return readUrl(new URL(url));
        } catch (MalformedURLException e) {
            throw new RuntimeException("Can't get url from string " + url);
        }
    }

    public static String readUrl(final URL url) {
        StringBuffer resultData = new StringBuffer();
        BufferedReader in = null;

        try {
            URLConnection urlConnection = url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                resultData.append(inputLine);
        } catch (Exception e) {
            throw new RuntimeException("Can't read  data from url " + url, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }

        return resultData.toString();
    }
}

Related

  1. readAsString(final URL url)
  2. readListFromURL(URL p_url)
  3. readPage(String url)
  4. readStringFromURL(String sourceURL)
  5. readUrl(final String strUrl)
  6. readUrl(final String url_str)
  7. readURL(String path)
  8. readUrl(String url_str)
  9. readUrl(String urlString)