Java URL Read getURL(final URL url)

Here you can find the source of getURL(final URL url)

Description

get URL

License

Open Source License

Declaration

public static String getURL(final URL url) throws IOException 

Method Source Code


//package com.java2s;
// Use and redistribution of this file is governed by the license terms in

import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    private static String FF_UA = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111317 Ubuntu/8.04 (hardy) Firefox/3.0.4";

    public static String getURL(final URL url) throws IOException {
        final HttpURLConnection conn = connect(url, false);
        final StringBuilder content = new StringBuilder();
        final InputStreamReader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
        final char[] buff = new char[1024];
        while (true) {
            final int blen = in.read(buff);
            if (blen < 0) {
                break;
            }/*  www .  java2 s .  co  m*/
            content.append(buff, 0, blen);
        }
        in.close();
        conn.disconnect();
        return content.toString();
    }

    public static HttpURLConnection connect(final URL url, final boolean output) throws IOException {
        final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", FF_UA);
        conn.setConnectTimeout(10000); // 10 seconds is reasonable
        conn.setReadTimeout(5000); // 5 seconds is reasonable
        conn.setDoOutput(output);
        conn.connect();
        return conn;
    }
}

Related

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