Java URL Connection getWebPageHtmlContent(String url)

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

Description

get Web Page Html Content

License

Open Source License

Parameter

Parameter Description
url The page url

Exception

Parameter Description

Return

The HTMl code of the page

Declaration

public static String getWebPageHtmlContent(String url) throws MalformedURLException, IOException 

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.io.StringWriter;

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

public class Main {
    /**/*from  w w w  .  ja va  2 s  .  co m*/
     *
     * @param url The page url
     * @return The HTMl code of the page 
     * @throws java.net.MalformedURLException Thrown when the URL is invalid
     */
    public static String getWebPageHtmlContent(String url) throws MalformedURLException, IOException {
        String line;
        try (final BufferedReader is = sendRequest(new URL(url))) {
            try (final StringWriter os = new StringWriter()) {
                while ((line = is.readLine()) != null) {
                    os.append(line);
                }
                return os.toString();
            }
        } catch (MalformedURLException e) {
            throw new MalformedURLException("Invalid URL " + url);
        } catch (IOException e) {
            throw new IOException("Error trying to write in the local buffer to store the page HTML from " + url,
                    e);
        }
    }

    private static BufferedReader sendRequest(URL url) throws IOException {
        URLConnection conn = url.openConnection();
        conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
        conn.addRequestProperty("Host", url.getHost());
        conn.connect();
        BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        return bf;
    }
}

Related

  1. getUrlEncoding(URLConnection connection)
  2. getURLForward(URL url)
  3. getURLInputStream(final URL url)
  4. getUrlSource(String url)
  5. getURLStream(URL url, int level)
  6. getWebsiteContents(URL url)
  7. httpPost(String urlString, String postPath, Map keyValuePairs)
  8. imageFromUrl(String url)
  9. isJarDirectory(JarURLConnection conn)