Java Tutorial
This java program is used to get the URL HTML content for the user input url.
This program connects to the URL provided by the user and reads the HTML content one line at a time.
If URL represents an HTTP resource, then connection is type casted as HttpConnection and reads the data.
import java.net.*; import java.util.Scanner; import java.io.*; public class URLHTMLContentTest{ static void getURLDetails(String urlLink){ try{ URL url = new URL(urlLink); URLConnection urlConnection = url.openConnection(); if(urlConnection instanceof HttpURLConnection){ HttpURLConnection connection = (HttpURLConnection)urlConnection; BufferedReader bufIn = new BufferedReader(new InputStreamReader(connection.getInputStream())); String content = ""; String line = ""; while((line=bufIn.readLine())!=null) { content += line; } System.out.println("URL Html Content: "+content); } }catch(IOException ex){ ex.printStackTrace(); } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.print("Enter URL: "); String urlLink = sc.next(); getURLDetails(urlLink); } }Output:
D:\Java_Programs>javac URLHTMLContentTest.java D:\Java_Programs>java URLHTMLContentTest Enter URL: https://google.com URL Html Content: ................................ (Complete HTML content will be displayed here.)
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page