Here you can find the source of getWebPageHtmlContent(String url)
Parameter | Description |
---|---|
url | The page url |
Parameter | Description |
---|
public static String getWebPageHtmlContent(String url) throws MalformedURLException, IOException
//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; } }