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



The Java HttpURLConnection getResponseMessage() method returns the HTTP response message, if any, returned along with the response code from a server. From responses like −

HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized

It extracts the Strings "OK" and "Not Found" respectively and returns null if none could be discerned from the responses (the result was not valid HTTP).

Declaration

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

public int getResponseMessage() throws IOException

Parameters

NA

Return Value

the HTTP response message, or null

Exception

IOException − if an error occurred connecting to the server.

Example 1

The following example shows the usage of Java HttpURLConnection getResponseMessage() 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 getResponseMessage(), we're checking the response message 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();
        
         String result = urlConnection.getResponseMessage();
         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: OK

Example 2

The following example shows the usage of Java HttpURLConnection getResponseMessage() 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 getResponseMessage(), 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();
        
         String result = urlConnection.getResponseMessage();
         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: OK

Example 3

The following example shows the usage of Java HttpURLConnection getResponseMessage() 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 getResponseMessage(), 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();
        
         String result = urlConnection.getResponseMessage();
         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: Not Found
java_httpurlconnection.htm
Advertisements