Updated April 12, 2023
Introduction to Java URLConnection
The URLConnection is a Java Programming Language class that usually represents one of the communication links or links between an URL and an application. This URLConnection class helps read and write the data to the specific/specified resource, which is actually referred to by an URL. It is one of the superclasses of all the classes. This URLConnection class’s instances are helpful to read from and to write, and it is to the resource referenced by the specific URL. Here connecting a connection to a specific URL is one type of multistep process.
Syntax
URLConnection openConnection()
Explanation: The open connection() method/function of the URL class will return the URLConnection class’s object.
How does the URLConnection work in Java
URLConnection class works by providing many methods. In the process of multi-steps of connecting an URL involves openConnection() and connect() methods. The openConnection() helps in manipulating the parameters which can affect the remote resource connection. The connect() method helps interacting which is having with the resource, and it is helpful for query header contents and fields.
The connection object is actually created just by invoking the openConnection method or function on an URL. The setup parameters of the connection object and the general request for the properties are to be manipulated. The actual and usual connection which is to the remote object is made with the help of connecting method usage. The remote object of it becomes available, and the header fields along with its contents of one of the remote object could be accessed. The getInputStream() method or function will help return all the data of the specific or specified URL in the particular stream, which can be used to read and display.
The URLConnection class of the Java Programming Language actually works by providing as many methods as need just to display all the data of the webpage or blog just with the help of getting InputStream() method or methods, but the getInputStream() method/function helps a lot in returning all the website data with the help of the specific URL which is mentioned in the stream. This URL will be used to read and used to display the source code of the website or a blog; to get all the source code, one has to use Loops for multiple types of source code display.
There are only two subclasses that extend the URLConnection Class of Java. They are HttpURLConnection and JarURLConnection. HttpURLConnection helps us connect to any type of URL that actually used the “HTTP” as its protocol; then, the HttpURLConnection class will be used. The JarURLConnection will help us trying to establish one of the connections to a specific jar file on the world wide web; then, the JarURLConnection will be used.
Methods
Some of the important methods are helpful in using to read or write or to get some info after the connection is established. They are:
1. URLConnection openConnection(): This method helps in opening the connection to the specific or specified URL.
2. Object getContent(): It will retrieve some content of URLConnection.
3. Map<String, List > getHeaderFields(): It will take the map which contains some values of various header fields in the specific HTTP folder.
4. getContentEncoding(): It will return some value of the content-encoding header’s field.
5. getContentLength(): It will return the content header field’s length.
6. getDate(): It will return header field’s date value
7. getHeaderField (int-i): It will return the header’s i-th index value
8. getHeaderField (String-Field): It will return the field named value “field” in some header which is to get a list of all the header fields.
9. OutputStream getOutputStream(): It will return one of the connection’s output stream.
10. InputStream getInputStream(): It will return one input stream to the open connection.
11. setAllowUserInteraction(boolean): It will set the setting as a TRUE value which means users can interact with the page. By default, the value of it is TRUE.
12. setDefaultUseCaches(boolean): It will set useCache field’s default as the provided value.
13. setDoInput(boolean): It will set only if the user is allowed to take a specific input or not
14. setDoInput(boolean): It will set only if the user now allows writing on the specific page. By default, its value is FALSE since, most of all, the URL doesn’t even allow writing.
Examples to Implement Java URLConnection
below is the example of implementing java URLConnection:
Example #1
This illustrates the reading and writing of a blog/website URL using the URLConnection class. At first, different types of java libraries are imported. Then the public class is created along with the public main method for java code filling. Then the URL variable is created to add the specific website/blog URL with the help of the URL command. Then “URLConnection” is used to open a connection to the above-mentioned URL. Then Map is used to get all fields map of the specific HTTP header. Then to print all the fields of website URL and their values, FOR LOOP is used. Then BufferedReader is used to get the open connection’s inputstream. Then to print source code line by line, WHILE LOOP is used. While loop will print all the source code, the website/blog url mentioned in the code itself.
code:
import java.io.*;
//importing java input output functions
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class URLConnectionclass1
{
public static void main(String[] args)
{
try
{
URL url1 = new URL("https://www.profitloops.in");
URLConnection urlcon1 = url1.openConnection();
Map<String, List<String>> header = urlcon1.getHeaderFields();
for (Map.Entry<String, List<String>> mp1 : header.entrySet())
{
System.out.print(mp1.getKey() + " : ");
System.out.println(mp1.getValue().toString());
}
System.out.println();
System.out.println("The Complete source code of the provided URL is-");
System.out.println(":---------------------------------:");
BufferedReader br1 = new BufferedReader(new InputStreamReader
(urlcon1.getInputStream()));
String i1;
while ((i1 = br1.readLine()) != null)
{
System.out.println(i1);
}
}
catch (Exception e1)
{
System.out.println(e1);
}
}
}
Output:
Conclusion
we hope you learned the definition of Java URLConnection and its syntax and explanation, How the URLConnection class works in Java Coding Language, and various examples to better understand the Java URLConnection concept and so easily.
Recommended Articles
This is a guide to Java URLConnection. Here we discuss an introduction to Java URLConnection, syntax, how does it work, methods and example. You can also go through our other related articles to learn more –