Course
URLConnection Class
Java Tutorial
This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.
URLConnection Class
Java URLConnection Class
java.net.URLConnection, is an abstract class whose subclasses represent the various types of URL connections. Instances of this class can be used both to read from and to write to the resource referenced by the URL.
For example −
- If you connect to a URL whose protocol is HTTP, the URL.openConnection() method returns an HttpURLConnection object.
- If you connect to a URL that represents a JAR file, the URL.openConnection() method returns a JarURLConnection object, etc.
Steps to make a connection to a URL
Following are the steps to make a connectiion to the URL and start processing.
- Invoke URL.openConnection() method to get connection object.
- Update setup parameters and general request properties as required using connection object various setter methods.
- Create a connection to remote object using connect() method of connection object.
- Once remote object is available, access the content/headers of the remote object.
URLConnection Class Declaration
public abstract class URLConnection extends Object
URLConnection Class Fields
URLConnection Class Methods
The URLConnection class has many methods for setting or determining information about the connection, including the following
Extends
This class extends following classes
- java.lang.Object
Example of URLConnection Class Methods
The following URLConnectionDemo program connects to a URL entered from the command line.
If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time.
package com.tutorialspoint;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;
public class URLConnectionDemo { public static void main(String [] args) { try { URL url = new URL("https://www.tutorialspoint.com"); URLConnection urlConnection = url.openConnection(); HttpURLConnection connection = null; if(urlConnection instanceof HttpURLConnection) { connection = (HttpURLConnection) urlConnection; }else { System.out.println("Please enter an HTTP URL."); return; } BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String urlString = ""; String current; while((current = in.readLine()) != null) { urlString += current; } System.out.println(urlString); } catch (IOException e) { e.printStackTrace(); } }}
A sample run of this program will produce the following result
Output
$ java URLConnectionDemo
.....a complete HTML content of home page of tutorialspoint.com.....