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 getContent(Class<?>[] classes) Method



Description

The Java URL getContent(Class<?>[] classes) method returns the contents of this URL. This method is a shorthand for. −

openConnection().getContent(classes)

Declaration

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

public final Object getContent(Class<?>[] classes)

Parameters

classes − an array of Java types

Return Value

the contents of this URL that is the first match of the types specified in the classes array. null if none of the requested types are supported.

Exception

IOException − if an I/O exception occurs.

Example 1

The following example shows the usage of Java URL getContent(Class<?>[] classes) method for a valid url. In this example, we're creating an instances of URL class. Now using getContent() method, we're getting the content and printing its type −

package com.tutorialspoint;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         Class<?>[] classes = {InputStream.class};
         Object content = url.getContent(classes);
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6df97b55

Example 2

The following example shows the usage of Java URL getContent(Class<?>[] classes) method for a invalid url. In this example, we're creating an instances of URL class. Now using getContent() method, we're are trying to get the content and an exception is raised and printed −

package com.tutorialspoint;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","index.htm");
         Class<?>[] classes = {InputStream.class};
         Object content = url.getContent(classes);
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.tutorialspoint.comindex.htm
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at java.net.URLConnection.getContent(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(Unknown Source)
	at java.net.URL.getContent(Unknown Source)
	at com.tutorialspoint.UrlDemo.main(UrlDemo.java:10)

Example 3

The following example shows the usage of Java URL getContent(Class<?>[] classes) method for a valid url. In this example, we're creating an instances of URL class using protocol, host and file name. Now using getContent() method, we're we're getting the content and printing its type then using openConnection() method, we're getting the content length and printed it in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         Class<?>[] classes = {InputStream.class};
         Object content = url.getContent(classes);
         System.out.println(content.getClass());
         System.out.println(url.openConnection().getContentLength());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

class sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
293637
java_url.htm
Advertisements