Java Program to Download the Web Page Content

Aim:

  • To download the web page content using java.

Requirement:

  • PC with UNIX/ Linux/windows Operating systems.
  • JAVA

Algorithm:

  • Start the program
  • Include necessary packages such as java.net., java.io., java.lan.Object, java.util.*
  • Create a object for URL class.
  • Read all content from the web page.
  • Print all the output
  • End the program

Program:

import java.net.*; import java.io.*; import java.util.*; class Main { public static void main(String args[]) throws Exception { int c; URL url=new URL("https://en.wikipedia.org/wiki/List_of_Xiaomi_products"); URLConnection ucon = url.openConnection(); System.out.println("Date " + new Date(ucon.getDate())); System.out.println("Content Type" +ucon.getContentType()); int len=ucon.getContentLength(); if(len > 0) { System.out.println(".....Content .... "); InputStream ip=ucon.getInputStream(); int i=len; while(((c=ip.read())!=-1) && --i>=0) { System.out.print((char)c); } ip.close(); } else System.out.println("No content, Page is empty"); } }
Code language: JavaScript (javascript)

Output:

Content Typetext/html; charset=UTF-8 .....Content .... <!DOCTYPE html><html class="client-nojs" lang="en" dir="ltr"> <head> <meta charset="UTF-8"/> <title>Listof Xiaomi products - Wikipedia</title> <script>document.documentElement.className="client-js";RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"b1779b12-b16f-4390-a79a-f160344f4faf","wgCSPNonce":false,"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"List_of_Xiaomi_products","wgTitle":"List of Xiaomi products","wgCurRevisionId":1067093645,"wgRevisionId":1067093645,"wgArticleId":57521914,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["CS1 maint: bot: original URL status unknown","CS1 Chinese (Hong Kong)-language sources (zh)","CS1 Chinese (China)-language sources (zh)","CS1 Chinese-language sources (zh)","CS1 maint: archived copy as title","CS1 Brazilian Portuguese-language sources (pt)","CS1 maint: unrecognized language","Articles with short description","Short description is different from Wikidat
Code language: HTML, XML (xml)

Result:

The download web page content using java is successfully executed.

Leave a Comment