Thursday, 14 January 2016

How To Connect to a Website and Display the Data of a Web Page Using JAVA

          JAVA provides "java.net" package which contains classes required for network programming. The networking in Java allows one person to communicate with other computers / systems. One of the core area under networking is connecting to different Websites and Servers. One may gather required data for his own study purposes. One may automate the same task to avoid the boredom process of connecting to a particular website. Though there are many possible applications of connecting with the websites I am just stating a few.


                 Today, Let's focus on the very basic task of connecting to a Website & Displaying its contents. Though one can do this by using many languages, I am going to discuss about how to do it using Java program. Following are the steps you need to understand this program.

  1. Create Buffered Reader Object To read Input from user.
  2. Read the input from the user.
  3. Open the connection to the web site by calling openConnection() of URL object.
  4. Fetch the contents of the resource by using an input stream.
  5. Print the data. You can store it to specific location if you want.

Following is the sample code of the implementation. You can also get it from my GitHub repository.

Expected output: You will see the data getting printed on the terminal itself.

Note: At "Step 2" when you type the URL; Please type it with the protocol.
Example: http://www.cstechera.in

// Title: To connect to a Website & Display the data of a Web Page
/**
 *
 * @author YogeshD
 */
import java.net.*;
import java.io.*;

class WebDisplay_Data
{
 public static void main(String args[ ]) throws MalformedURLException, IOException
 {
  // Step 1: Create Buffered Reader Object To read Input from user
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
  // Step 2: Read the input from the user
  // e.g.: http://www.cstechera.in
  System.out.print("Enter the url of website :- ");
  String url = br.readLine();
  
  // Step 3: Open the connection to the web site by calling openConnection() of URL object
  URL url_adr = new URL(url);
  URLConnection urlc = url_adr.openConnection();
  
  // Additional check to prevent un-necessary execution. Check the length of URL Object
  int len = urlc.getContentLength();
  if (len == 0)
   System.out.println("Resource is not available");
  else
  {
   // Step 4: Fetch the contents of the resource by using an input stream
   InputStream ip = urlc.getInputStream();
   System.out.println("Contents are :- ");
   while (true)
   {
    int no = ip.read();
    if (no == -1)
     break;
    // Step 5: Print the data. You can store it to specific location if you want.
    System.out.print((char) no);
   }
   ip.close();
  }
  br.close();
 }
} 




==> Posted By Yogesh B. Desai


Next Post: Java Home Page

No comments:

Post a comment