Java HTTP Read getHtmlContent(String url)

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

Description

get Html Content

License

Open Source License

Declaration

public static String getHtmlContent(String url) 

Method Source Code

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getHtmlContent(String url) {
        return getHtmlContent(getConnection(url));
    }/*w w w. j a  v  a2s .  c o  m*/

    public static String getHtmlContent(HttpURLConnection h) {
        try {
            return h == null ? null : getInputStreamContent(h
                    .getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static HttpURLConnection getConnection(String url) {
        try {
            return (HttpURLConnection) new URL(
                    url.startsWith("http://") ? url : "http://" + url)
                    .openConnection();
        } catch (Exception e) {

        }
        return null;
    }

    public static String getInputStreamContent(InputStream i) {
        return getInputStreamContent(i, "UTF-8");
    }

    public static String getInputStreamContent(InputStream i,
            String encoding) {
        if (i != null) {
            BufferedReader pBr;
            try {
                pBr = new BufferedReader(new InputStreamReader(i, encoding));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return null;
            }
            StringBuffer pSb = new StringBuffer();
            int a = -1;
            try {
                while ((a = pBr.read()) != -1) {
                    pSb.append((char) a);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return pSb.toString();
        }
        return null;
    }
}

Related

  1. getContentTypeFromUrl(String urlname)
  2. getContentTypeSpecified(URLConnection connection)
  3. getContentWithPost(String urls, Map pv)
  4. getData(String urlString)
  5. getHtmlByteArray(final String url)
  6. getInputStream(HttpURLConnection conn)
  7. getInputStream(HttpURLConnection connection)
  8. getInputStream(String myUrl)
  9. getInputStream(String url)