Tuesday 23 February 2016

How to read data from URL in java

Java is a best programming language i known. It is complete and easy. Java networking can used for making chat programs, browsers, email sender and many more applications. Here i am going to show you how we can read website data using URL. Java uses URL class for this purpose.
                                                            The URL class is used to represent resources on the World-Wide Web. Every resource has an address, which identifies it uniquely and contains enough information for a Web browser to find the resource on the network and retrieve it. The address is called a “url” or “universal resource locator”.  (URLs can actually refer to resources from other sources besides the web; after all, they are “universal”.)
                                                             An object belonging to the URL class represents such an address. Once you have a URL object, you can use it to open a URLConnection to the resource at that address. A URL is ordinarily specified as a string, such as “http://math.hws.edu/eck/index.html”. There are also relative URL’s. A relative URL specifies the location of a resource relative to the location of another URL, which is called the base or context for the relative URL. 
                                                              Once you have a valid URL object, you can call its openConnection() method to set up a connection. This method returns a URLConnection. The URLConnection object can, in turn, be used to create an InputStream for reading data from the resource represented by the URL. This is done by calling its getInputStream() method. The openConnection() and getInputStream() methods can both throw exceptions of type IOException. Once the InputStream has been created, you can read from it in the usual way, including wrapping it in another input stream type, such as BufferedReader, or using a Scanner. Reading from the stream can, of course, generate exceptions.
                                                               Let’s look at a short example that uses all this to read the data from a URL. This subroutine opens a connection to a specified URL, checks that the type of data at the URL is text, and then copies the text onto the screen. 

import java.net.*;
import java.util.Scanner;
import java.io.*;


public class amar {

    public static void main(String[] args) {
        String url;   
        String urlLC; 
        if (args.length == 0) {
            Scanner stdin = new Scanner(System.in);
            System.out.print("Enter a url: ");
            url = stdin.nextLine();
        }
        else {
            url = args[0];
        }
        urlLC = url.toLowerCase();
        if ( ! (urlLC.startsWith("http://") || urlLC.startsWith("ftp://") ||
                urlLC.startsWith("file://"))) {
            url = "http://" + url;
            System.out.println("Using: " + url);
        }
        System.out.println();
        try {
            readTextFromURL(url);
        }
        catch (IOException e) {
            System.out.println("\n*** Sorry, an error has occurred ***\n");
            System.out.println(e);
            System.out.println();
        } 
    }
 

    static void readTextFromURL( String urlString ) throws IOException {



        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();
        InputStream urlData = connection.getInputStream();
  

        String contentType = connection.getContentType();
        System.out.println("Stream opened with content type: " + contentType);
        System.out.println();
        if (contentType == null || contentType.startsWith("text") == false) throw new               IOException("URL does not seem to refer to a text file.");
        System.out.println("Fetching context from " + urlString + " ...");
        System.out.println(); 

        BufferedReader in; 
        in = new BufferedReader( new InputStreamReader(urlData) );

        while (true) {
            String line = in.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        in.close();

       }
      }  

In this program a prompt will come and ask to enter the URL starting with "http://", "ftp://", or "file://". If it does not start with one of these protocols, "http://" is added to the beginning of the input line. 
                                                  If everything will we fine then the text from the URL is copied to the console window of eclipse.
Good Luck guys.