Java - Reading From a URL using URLConnection Class
Last Updated :
24 Apr, 2022
URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class
- HttpURLConnection: If we are connecting to any URL which uses "http" as its protocol, then HttpURLConnection class is used.
- JarURLConnection: If however, we are trying to establish a connection to a jar file on the web, then JarURLConnection is used.
Once the connection is established and we have a URLConnection object, we can use it to read or write or get further information about when was the page/file last modified, content length, etc.
But barely getting the state information is not the true motive of a real-world application. To retrieve the information, process it, and send the results back to the server, or just display the required information retrieved from the server is what we are aiming at.
Illustration:
Small scale application which asks for a movie name from the user and in turn returns the "IMDb" rating of the movie or returns all the links related to that movie. All of this can be achieved using the URLConnection class.
Methods of URLConnection Class
Method | Action Performed |
---|
getContent() | Retrieves the content of the URLConnection |
getContentEncoding() | Returns the value of the content-encoding header field. |
getContentLength() | Returns the length of the content header field |
getDate() | Returns the value of date in the header field |
getHeaderFields() | Returns the map containing the values of various header fields in the HTTP header |
getHeaderField(int i) | Returns the value of the ith index of the header |
getHeaderField(String field) | Returns the value of the field named "field" in the header |
getInputStream() | Returns the input stream to this open connection been inside of OutputStream class |
getOutputStream() | Returns the output stream to this connection of OutputStream class |
openConnection() | Opens the connection to the specified URL. |
setAllowUserInteraction() | Setting this true means a user can interact with the page. The default value is true. |
setDefaultUseCaches() | Sets the default value of useCache field as the given value. |
setDoInput() | Sets if the user is allowed to take input or not |
setDoOutput() | Sets if the user is allowed to write on the page. The default value is false since most of the URLs don't allow to write |
Now after having an understanding of the methods Steps involved in the above process
- URL Creation: Create a URL object using any of the constructors given
- Create Object: Invoke the openConnection() call to create the object of URLConnection.
- Display the Content: Either use the above-created object to display the information about the resource or to read/write contents of the file to the console using bufferedReader and InputStream of the open connection using getInputStream() method.
- Close Stream: Close the InputStream when done.
Implementation: Let us look at a sample program, which uses the above methods to display the header fields and also print the source code of the entire page onto the console window.
Java
// Java Program to Illustrate Reading and Writing
// in URLConnection Class
// Importing required classes
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Class
// URLConnectionclass
class GFG {
// main driver method
public static void main(String[] args)
{
// Try block to check for exceptions
try {
// Fetching URL by creating
URL url = new URL(
"https://www.geeksforgeeks.org/java");
// Opening the connection to the above URL
URLConnection urlcon = url.openConnection();
// Executing the below line would print the
// value of
// Allow User Interaction field.
// System.out.println(urlcon.getAllowUserInteraction());
// Executing the below line would print
// the value of Content Type field.
// System.out.println(urlcon.getContentType());
// Executing the below line would print the
// value
// of URL of the given connection.
// System.out.println(urlcon.getURL());
// Executing the below line would
// print the value of Do Input field.
// System.out.println(urlcon.getDoInput());
// Executing the below line would
// print the value of Do Output field.
// System.out.println(urlcon.getDoOutput());
// Executing the below line would
// print the value of Last Modified field.
// System.out.println(new
// Date(urlcon.getLastModified()));
// Executing the below line would
// print the value of Content Encoding field.
// System.out.println(urlcon.getContentEncoding());
// To get a map of all the fields of http header
Map<String, List<String> > header
= urlcon.getHeaderFields();
// Printing all the fields along with their
// value
for (Map.Entry<String, List<String> > mp :
header.entrySet()) {
System.out.print(mp.getKey() + " : ");
System.out.println(
mp.getValue().toString());
}
System.out.println();
System.out.println(
"Complete source code of the URL is-");
System.out.println(
"---------------------------------");
// Getting the inputstream of the open
// connection
BufferedReader br
= new BufferedReader(new InputStreamReader(
urlcon.getInputStream()));
String i;
// Printing the source code line by line
while ((i = br.readLine()) != null) {
System.out.println(i);
}
}
// Catch block to handle exceptions
catch (Exception e) {
// Displaying exceptions
System.out.println(e);
}
}
}
Output:
Keep-Alive : [timeout=5, max=100]
null : [HTTP/1.1 200 OK]
Server : [Apache/2.4.18 (Ubuntu)]
Connection : [Keep-Alive]
Last-Modified : [Wed, 16 Nov 2016 06:49:55 GMT]
Date : [Wed, 16 Nov 2016 10:58:34 GMT]
Accept-Ranges : [bytes]
Cache-Control : [max-age=3]
ETag : ["10866-541657b07e4d7"]
Vary : [Accept-Encoding]
Expires : [Wed, 16 Nov 2016 10:58:37 GMT]
Content-Length : [67686]
Content-Type : [text charset="UTF-8" language="/html;"][/text]
Complete source code of the URL is-
--------------------------------------------------
...source code of the page...
Similar Reads
java.net.URLConnection Class in Java
URLConnection Class in Java is an abstract class that represents a connection of a resource as specified by the corresponding URL. It is imported by the java.net package. The URLConnection class is utilized for serving two different yet related purposes, Firstly it provides control on interaction wi
5 min read
Java.net.JarURLConnection class in Java
Prerequisite - JAR files in Java What is a Jar file? JavaArchive(JAR) bundles all the classes in one package. Since the archive is compressed and can be downloaded in a single HTTP connection, it is often faster to download the archive than to download individual classes. Although jar bundles all th
4 min read
URL Rewriting using Java Servlet
Url rewriting is a process of appending or modifying any url structure while loading a page. The request made by client is always a new request and the server can not identify whether the current request is send by a new client or the previous same client. Due to This property of HTTP protocol and W
5 min read
Java Program to Goto a Link Using Applet
In this article, we shall be animating the applet window to show the goto link in Java Applet. A Goto link is basically, any particular link that redirects us from one page to another. Approach to Implement Goto a Link using AppletCreate an applet with the two buttons.Add ActionListener to the butto
2 min read
java.net.NetPermission Class in Java
NetPermission class is used to allow network permissions. NetPermission class extends BasicPermission class. It is a ânamedâ permission i.e it contains a name but no action. Permission nameWhat permission allowsRisks associated with this permissionallowHttpTraceThis permission allows using the HTTP
4 min read
java.net.URL Class in Java
URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part
4 min read
How to Read an HTTP Response Body as a String in Java?
When working with the HTTP requests in Java it's common to need to read the response body returned by the server. In many cases, you may want to read the response body as a string to easily parse and manipulate the data. In this article, we'll explore how to read an HTTP response body as a string in
3 min read
Java.net.URLEncoder class in Java
This class is a utility class for HTML form encoding. Encoding makes the form of URL more reliable and secure. When the user request is triggered by a get method, the form parameters and their values are appended at the end of URL after a '?' sign. The problem arises when special characters are used
3 min read
Java.net.URLDecoder class in Java
This is a utility class for HTML form decoding. It just performs the reverse of what URLEncoder class do, i.e. given an encoded string, it decodes it using the scheme specified. Generally when accessing the contents of request using getParameter() method in servlet programming, the values are automa
2 min read
Java.io.ObjectInputStream Class in Java | Set 2
Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea
6 min read