What is the importance of URLConnection class? Explain with suitable Java code.

URLConnection Class

The Java URLConnection class represents a communication link between the URL and the application. This class can be used to read and write data to the specified resource referred by the URL. It is not possible to instantiate the URLConnection class directly. Instead we have to create the URLConnection via the openConnection() method of URL class. 

Syntax for this is given below:

public URLConnection openConnection() throws IOException{ }

The URLConnection class provides many methods, we can display all the data of a v using the getInputStream() method. The getInputStream() method returns all the data of the specified URL in the stream that can be read and displayed.

Example

import java.io.*;

import java.net.*; public class URLConnectionEx

{

public static void main(String[] args)

{

try

{

URL url =new URL("http://www.tutorialspoint.com/

java/java_networking.html");

URL Connection urlcon=url.openConnection();

InputStream stream=urlcon.getInputStream(); int i; while ((i-stream.read()) !=-1)

{

}

System.out.print((char)i);

}

catch(Exception e)

{

}

System.out.println(e);

}

Sample Output

import java.net.*;

import java.io.*;

public class GreetingServer extends Thread

{

private ServerSocket serverSocket;

public GreetingServer(int port) throws IOException

serverSocket = new ServerSocket (port); serverSocket.setSoTimeout (10000);

}

 Java URLConnection Class

The Java URLConnection class represents a communication link between the URL and the application. It can be used to read and write data to the specified resource referred by the URL.

Features of URLConnection class

  • URLConnection is an abstract class. The two subclasses HttpURLConnection and JarURLConnection make the connection between the client Java program and URL resource on the internet.
  • With the help of URLConnection class, a user can read and write to and from any resource referenced by an URL object.
  • Once a connection is established and the Java program has an URLConnection object, we can use it to read or write or get further information like content length, etc.


Constructor

protected URLConnection(URL url)//It constructs a URL connection to the specified URL.


How to get the object of URLConnection Class

The openConnection() method of the URL class returns the object of URLConnection class.


Syntax:

public URLConnection openConnection()throws IOException{}    


Displaying Source Code of a Webpage by URLConnecton Class

The URLConnection class provides many methods. We can display all the data of a webpage by using the getInputStream() method. It returns all the data of the specified URL in the stream that can be read and displayed.

Example of Java URLConnection Class

import java.io.*;    

import java.net.*;    

public class URLConnectionExample {    

public static void main(String[] args){    

try{    

URL url=new URL("http://www.javatpoint.com/java-tutorial");    

URLConnection urlcon=url.openConnection();    

InputStream stream=urlcon.getInputStream();    

int i;    

while((i=stream.read())!=-1){    

System.out.print((char)i);    

}    

}catch(Exception e){System.out.println(e);}    

}    

}    


Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?