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 getResponseCode() Method



The Java HttpURLConnection getResponseCode() method returns the status code from an HTTP response message. For example, in the case of the following status lines −

HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized

It will return 200 and 401 respectively. Returns -1 if no code can be computed from the response (i.e., the response is not valid HTTP).

Declaration

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

public int getResponseCode() throws IOException

Parameters

NA

Return Value

the HTTP Status-Code, or -1

Exception

IOException − if an error occurred connecting to the server.

Example 1

The following example shows the usage of Java HttpURLConnection getResponseCode() method for a valid 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 and then we're connecting to the url by calling the connect() method. Using getResponseCode(), we're checking the response code and printing the same −

package com.tutorialspoint;

import java.io.IOException;
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/index.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        
         int result = urlConnection.getResponseCode();
         System.out.println("response code: " + result);  
      
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

response code: 200

Example 2

The following example shows the usage of Java HttpURLConnection getResponseCode() method for a valid 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 and then we're connecting to the url by calling the connect() method. Using getResponseCode(), we're checking the response code and printing the same −

package com.tutorialspoint;

import java.io.IOException;
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/index.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        
         int result = urlConnection.getResponseCode();
         System.out.println("response code: " + result);  
      
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

response code: 200

Example 3

The following example shows the usage of Java HttpURLConnection getResponseCode() method for an 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 and then we're connecting to the url by calling the connect() method. Using getResponseCode(), we're checking the response code and printing the same −

package com.tutorialspoint;

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

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com/index1.htm");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        
         int result = urlConnection.getResponseCode();
         System.out.println("response code: " + result);  
      
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

response code: 404
java_httpurlconnection.htm
Advertisements