Java URL Read readPage(String url)

Here you can find the source of readPage(String url)

Description

read Page

License

Open Source License

Declaration

public static String[] readPage(String url) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import java.util.ArrayList;

public class Main {
    public static String[] readPage(String url) {
        url = url.replaceAll(" ", "%20");
        ArrayList<String> lines = new ArrayList<String>();
        try {//w  ww .j a  va  2 s.  c  om
            final URLConnection con = createURLConnection(url);
            final BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                lines.add(line);
            }
            in.close();
        } catch (Exception e) {
            System.out.println("Error reading page!");
        }
        return lines.toArray(new String[lines.size()]);
    }

    public static URLConnection createURLConnection(String url) {
        try {
            final URL address = new URL(url);
            final URLConnection connection = address.openConnection();
            connection.addRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
            connection.setConnectTimeout(5000);
            connection.setRequestProperty("Content-Type", "image/png");
            return connection;
        } catch (IOException ex) {
            System.out.println("Error creating connection!");
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. getUrlSource(String url)
  2. getUrlStatus(String url)
  3. getUrlTxt(String url)
  4. readAsString(final URL url)
  5. readListFromURL(URL p_url)
  6. readStringFromURL(String sourceURL)
  7. readUrl(final String strUrl)
  8. readUrl(final String url)
  9. readUrl(final String url_str)