URLconnection java example code

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

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


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.
The getInputStream() method returns all the data of the
specified URL in the stream that can be read and displayed.


 Methods using in  URLConnection class



public URLConnection openConnection() It returns the instance of URLConnection i.e. associated with this URL
public String getContentType( ) This method returns the MIME content type of the data. It relies on the web server to send a valid content type.like text/plain, image/gif, application/xml, and image/jpg.
public int getContentLength( ) The get Content Length( ) method tells us how many bytes there are in the content.
public long getDate( ) The getDate( ) method returns a long that tells us when the document was sent, in milliseconds since midnight, Greenwich Mean Time (GMT), January 1, 1970. We can convert it to a java.util.Date package
public long getExpiration( ) Some documents have server -based expiration dates that indicate when the document should be deleted from the cache and reloaded from the server.
public long getLastModified( ) The final date method, getLastModified( ), returns the date on which the document was last modified.

Program  Source code

Output of URLConnectionClass

e:>javac URLConnectionDemo.java
e:>java   URLConnectionDemo











(Visited 126 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image