Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java URLConnection

Introduction to Java URLConnection

The Urlconnection is a class of Java Programming Language which usually represents one of the communication links or links between an URL and an application. This URLConnection class is very much helpful in reading and writing the data to the specific/specified resource which is actually referred by an URL. It is one of the superclasses of all the classes. The instances of this URLConnection class is 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 object of the URLConnection class.

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 in returning all the data of the specific or specified URL in the particular stream which can be used to read and to be displayed.

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 also used to display what is the source code of the website or a blog. In order to get all the source code one to have 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 in connecting to any type of URL which 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.

There are some of the important methods which 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 getHeaderFields(): It will 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 some 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 to implement java URLConnection:

Example #1

This is the example of illustrating 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> header = urlcon1.getHeaderFields();
for (Map.Entry> 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 what is the definition of Java URLConnection along with its syntax and explanation, How the URLConnection class works in Java Coding Language along with various examples to under the Java URLConnection concept better 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 –

  1. Optional Class in Java 8
  2. Disable JavaScript
  3. EJB in Java
  4. Java min()

The post Java URLConnection appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Java URLConnection

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×