Java URL Connection slurpURL(URL u, String encoding)

Here you can find the source of slurpURL(URL u, String encoding)

Description

Returns all the text at the given URL.

License

Open Source License

Declaration

public static String slurpURL(URL u, String encoding) throws IOException 

Method Source Code


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

import java.io.*;

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

public class Main {
    /**/*from   www  .j  a v a  2s .co m*/
     * Returns all the text at the given URL.
     */
    public static String slurpURL(URL u, String encoding) throws IOException {
        String lineSeparator = System.getProperty("line.separator");
        URLConnection uc = u.openConnection();
        uc.setReadTimeout(30000);
        InputStream is;
        try {
            is = uc.getInputStream();
        } catch (SocketTimeoutException e) {
            // e.printStackTrace();
            System.err.println("Time out. Return empty string");
            return "";
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
        String temp;
        StringBuilder buff = new StringBuilder(16000); // make biggish
        while ((temp = br.readLine()) != null) {
            buff.append(temp);
            buff.append(lineSeparator);
        }
        br.close();
        return buff.toString();
    }

    /**
     * Returns all the text at the given URL.
     */
    public static String slurpURL(URL u) throws IOException {
        String lineSeparator = System.getProperty("line.separator");
        URLConnection uc = u.openConnection();
        InputStream is = uc.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String temp;
        StringBuilder buff = new StringBuilder(16000); // make biggish
        while ((temp = br.readLine()) != null) {
            buff.append(temp);
            buff.append(lineSeparator);
        }
        br.close();
        return buff.toString();
    }

    /**
     * Returns all the text at the given URL.
     */
    public static String slurpURL(String path) throws Exception {
        return slurpURL(new URL(path));
    }
}

Related

  1. setTimeout(URLConnection conn, int milliseconds)
  2. setTimeouts(URLConnection connection)
  3. shortenUrl(String url)
  4. skip(URLConnection connection, long count)
  5. slurpStream(URLConnection conn)
  6. slurpURLNoExceptions(URL u)
  7. url2list(String sURL, String encoding)
  8. urlContents(String urlString)
  9. urlExists(String urlString)