Java HTTP Read getContentFromURL(String url)

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

Description

get Content From URL

License

Creative Commons License

Declaration

public static String getContentFromURL(String url) 

Method Source Code

//package com.java2s;
/**//from  w  w  w  .j  av a2  s .  c o  m
 * This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0
 * International License. http://creativecommons.org/licenses/by-nd/4.0/
 *
 * @author Wruczek
 */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.Reader;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static String getContentFromURL(String url) {
        try {
            return getContentFromURL(new URL(url));
        } catch (Exception e) {
        }
        return null;
    }

    public static String getContentFromURL(URL url) {
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setDoOutput(true);

            Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0;)
                sb.append((char) c);

            return sb.toString().trim();
        } catch (IOException e) {

        }
        return null;
    }
}

Related

  1. getContent(String urlStr)
  2. getContentAsString(URL url)
  3. getContentEncoding(URL url)
  4. getContentFromHttpGetBasicAuth(String urlString, String username, String password)
  5. getContentFromURL(String sURL)
  6. getContentLength(URL url)
  7. getContentLength(URLConnection con)
  8. getContents(String urlStr)
  9. getContentTypeFromUrl(String urlname)