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 - HttpURLConnection getErrorStream()



The Java HttpURLConnection getErrorStream() method returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.

This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.

Declaration

Following is the declaration for java.net.HttpURLConnection.getErrorStream() method

public InputStream getErrorStream()

Parameters

NA

Return Value

an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.

Exception

NA

Example 1

The following example shows the usage of Java HttpURLConnection getErrorStream() method for a invalid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getErrorStream(), we're checking if server is returning an error response and printing 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;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Connected.
Error Stream is null

Example 2

The following example shows the usage of Java HttpURLConnection getErrorStream() method for a invalid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getErrorStream(), we're checking if server is returning an error response and printing 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;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Connected.
Error Stream is null

Example 3

The following example shows the usage of Java HttpURLConnection getErrorStream() method for a invalid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getErrorStream(), we're checking if server is returning an error response and printing 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;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.google.com/index1.htm");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Connected.
Error Stream is null
java_httpurlconnection.htm
Advertisements