Java URL Connection urlContents(String urlString)

Here you can find the source of urlContents(String urlString)

Description

url Contents

License

Open Source License

Declaration

public static String urlContents(String urlString) 

Method Source Code

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

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

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

public class Main {
    public static String urlContents(String urlString) {
        try {//from   www  .  j av  a2s.co m
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            // following needed?
            //       connection.setRequestProperty("User-Agent", ???);
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            return bufferedReaderToString(new BufferedReader(new InputStreamReader(connection.getInputStream())));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String bufferedReaderToString(BufferedReader in) throws IOException {
        StringBuilder contents = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            contents.append(line + "\r");
        }
        in.close();
        return contents.toString();
    }
}

Related

  1. skip(URLConnection connection, long count)
  2. slurpStream(URLConnection conn)
  3. slurpURL(URL u, String encoding)
  4. slurpURLNoExceptions(URL u)
  5. url2list(String sURL, String encoding)
  6. urlExists(String urlString)
  7. URLExists(URL url, StringBuffer errorMsg)
  8. urlifyMap(Long nid, double latitude, double longitude)
  9. urlToHtm(String word, String findurl, String path)