Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL openConnection() Method with Examples



Description

The Java URL openConnection() method returns a URLConnection instance representing a connection to the remote object referred to by the URL. A new instance of URLConnection is created every time when invoking the URLStreamHandler.openConnection(URL) method of the protocol handler for this URL. When URLConnection.connect() method is called than URLConnection instance establishes the actual network connection. For HTTP protocol, an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

Declaration

Following is the declaration for java.net.URL.openConnection() method

public URLConnection openConnection()

Parameters

NA

Return Value

a URLConnection linking to the URL.

Exception

IOException − if an I/O exception occurs.

Example 1

The following example shows the usage of Java URL openConnection() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url and printing the contents of the same −

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 UrlDemo {
   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();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>

Example 2

The following example shows the usage of Java URL openConnection() method for a valid url. In this example, we're creating an instance of URL class with http protocol. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url and printing the contents of the same −

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 UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http","www.tutorialspoint.com","/index.htm");
         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();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>

Example 3

The following example shows the usage of Java URL openConnection() method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url. As protocol is not http or https, program will print a message Please enter an HTTP URL. and terminates as evident below −

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 UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm");
         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();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Please enter an HTTP URL.
java_url.htm
Advertisements