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
Post a Comment