Java URL Connection slurpURLNoExceptions(URL u)

Here you can find the source of slurpURLNoExceptions(URL u)

Description

Returns all the text at the given URL.

License

Apache License

Declaration

public static String slurpURLNoExceptions(URL u) 

Method Source Code


//package com.java2s;
/*/*from   w  w w. jav  a2 s .  co  m*/
 *  * Copyright 2016 Skymind, Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 */

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

public class Main {
    /**
     * Returns all the text at the given URL.
     */
    public static String slurpURLNoExceptions(URL u, String encoding) {
        try {
            return slurpURL(u, encoding);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Returns all the text at the given URL.
     */
    public static String slurpURLNoExceptions(URL u) {
        try {
            return slurpURL(u);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Returns all the text at the given URL. If the file cannot be read (non-existent, etc.),
     * then and only then the method returns <code>null</code>.
     */
    public static String slurpURLNoExceptions(String path) {
        try {
            return slurpURL(path);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 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. setTimeouts(URLConnection connection)
  2. shortenUrl(String url)
  3. skip(URLConnection connection, long count)
  4. slurpStream(URLConnection conn)
  5. slurpURL(URL u, String encoding)
  6. url2list(String sURL, String encoding)
  7. urlContents(String urlString)
  8. urlExists(String urlString)
  9. URLExists(URL url, StringBuffer errorMsg)