How to send HTTP GET request with Java without using any external libraries

There was this time when I need to build a program to check whether my web server is running fine. To determine that my web server is running fine, I wrote a Java applet that sends a HTTP GET to one of my web resources when the applet runs for the first time. And to keep the applet light, I look into Java in-built features for sending HTTP GETs to my server. This post documents a proof of concept that I did to communicate with my server endpoint via HTTP GET using Java in-built features.

Defining the server endpoint to connect to

I chose to use my robots.txt for probing whether my server is running fine. To probe my server, I write the following codes:

URL robotsUrl = new URL("http://www.techcoil.com/robots.txt");
HttpURLConnection urlConnection = (HttpURLConnection) robotsUrl.openConnection();  

I first create a java.net.URL object to my robots.txt. I then call robotsUrl.openConnection() to get a java.net.HttpURLConnection object that will allow me to send a HTTP request to my server to get robots.txt as a HTTP response.

Configuring the HttpURLConnection

Unlike sending a HTTP POST to the server endpoint, no additional configurations is needed for the HttpURLConnection object as the default request method is set to HTTP GET and we do not need to write anything as the HTTP request body.

Sending the HTTP request and verifying the HTTP response

The HTTP request will be sent to the server when we read from our HttpURLConnection object. As such, the next step will be to read the HTTP response body from urlConnection and verify the contents. The contents should contain the string "Sitemap: http://www.techcoil.com/sitemap-index.xml".

To do so, I wrote the following codes:

Scanner httpResponseBodyScanner = new Scanner(urlConnection.getInputStream());

// Use a ByteArrayOutputStream to store the contents of the HTTP response body
ByteArrayOutputStream responseBodyBaos = new ByteArrayOutputStream();
while(httpResponseBodyScanner.hasNextLine()) {
	responseBodyBaos.write(httpResponseBodyScanner.nextLine().getBytes());
}
responseBodyBaos.close();
httpResponseBodyScanner.close();

// Verify contents of robots.txt
String robotsContent = responseBodyBaos.toString();
if (robotsContent.trim().equals("Sitemap: http://www.techcoil.com/sitemap-index.xml")) {
	System.out.println("Able to retrieve robots.txt from server. Server is running fine.");
}
else {
	System.out.println("Not able to retrive robots.txt from server.");
}

I first layered a java.util.Scanner object to the input stream of urlConnection to allow my codes to read the HTTP response body that my server sends back to me. I then use a java.io.ByteArrayOutputStream object to record the contents of the HTTP response body.

After all the contents of my HTTP response body is recorded in the java.io.ByteArrayOutputStream object, I proceed to verify the contents, trimming any leading or trailing white spaces. If the trimmed contents is the string "Sitemap: http://www.techcoil.com/sitemap-index.xml", I output "Able to retrieve robots.txt from server. Server is running fine." to console. If not, I output "Not able to retrive robots.txt from server." to console.

A simple console application that sends a HTTP GET request to a server endpoint at Techcoil to check whether Techcoil is running fine

To sum up the points covered in this post, I put together the following class which can be used to build a console application that checks the content of http://www.techcoil.com/robots.txt to determine whether Techcoil is running fine:

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class Program {

	public static void main(String[] args) {
		
		ByteArrayOutputStream responseBodyBaos = null;
        Scanner httpResponseBodyScanner = null;
        try {
            // Define server endpoint
            URL robotsUrl = new URL("http://www.techcoil.com/robots.txt");
            HttpURLConnection urlConnection = (HttpURLConnection) robotsUrl.openConnection(); 
 
            httpResponseBodyScanner = new Scanner(urlConnection.getInputStream());
 
            // Use a ByteArrayOutputStream to store the contents of the HTTP response body
            responseBodyBaos = new ByteArrayOutputStream();
            while(httpResponseBodyScanner.hasNextLine()) {
                responseBodyBaos.write(httpResponseBodyScanner.nextLine().getBytes());
            }
            responseBodyBaos.close();
            httpResponseBodyScanner.close();
 
            // Verify contents of robots.txt
            String robotsContent = responseBodyBaos.toString();
            if (robotsContent.trim().equals("Sitemap: http://www.techcoil.com/sitemap-index.xml")) {
                System.out.println("Able to retrieve robots.txt from server. Server is running fine.");
            }
            else {
                System.out.println("Not able to retrive robots.txt from server.");
            }
 
        } catch(IOException ioException) {
            System.out.println("IOException occurred while contacting server.");
        } finally {
            if (responseBodyBaos != null) {
            	try {
                responseBodyBaos.close();
            	} catch (IOException ioe) {
            		System.out.println("Error while closing response body stream");
            	}
            }
            if (httpResponseBodyScanner != null) {
                httpResponseBodyScanner.close();
            }
        }
}

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.