Java HTTP Call

This tutorial shows a basic “Getting started” Java program but as always, we would like to add something practical. In this case, we would make an HTTP call using HttpURLConnection, retrieve the results and display them.

Project Setup: Please note this example is built on top of a servlet project and a tutorial for this can be found here . You can make a HTTP call from any program (servlet, jsp webapp, java console app, or android etc) but we just leveraged an existing project.

Most tutorials start with a Hello World example, but instead of that, we will attempt an HTTP Call and output our results instead. There are a variety of practical uses for this such as:

  • Making service calls to servlets or REST services
  • Retrieving data from another URL
  • Getting HTML from a website for Screen Scraping
  • Overcoming client-side cross-domain calls with a server side call

1. HttpURLConnection

There are many ways to make an HTTP call but using is one of the most common and simplest ways. Other options include Apache HTTP Components’ HttpClient or natively

The following example in this tutorial can be briefly summarized as:


1. Picking a destination URL

Since we are making an HTTP call, we will need a destination URL. You can choose to pick a URL that returns XML data, a webpage (HTML), or in our case, a JSON resource. As provided in the snippet below, the destination URL is set and passed onto a helper method to make the call. This method, httpCall is provided later in the article.


2. httpCall Helper Method

Provided below is the helper method ‘httpCall’. It will take in the destination URL and a method (those familiar with the HTTP protocol will know the method as one of the following; GET/POST/PUT/DELETE). This method also returns the content at the destination. The code snippet provided below is a short example of using HttpURLConnection. There are a lot more options such as setting the connection and/or read timeouts, caching options and more. We will discuss these after this example.


3. Reading Destination Content – InputStream

In step two during the HTTP call itself, there is another helper method called, Utils.getStringFromInputStream. This is simply another helper method we have defined to assist us with InputStreams, an abstract way to read input bytes from different sources (Files, Service Calls, Web Pages, other data structures) For our purpose, we need to read the bytes and convert it to a String.


4. Printing the output

The final result of the content read is returned and finally displayed to the screen as seen in the first snippet (PrintWriter) Since this is built upon a simple servlet that i have setup on a local Tomcat 7 server, this is the result when I call the servlet from the browser.

HTTP Call - JSON output


Next Steps

Now that we have completed a simple HTTP call with a simple JSON destination URL, we will go into more detail in regards to the HttpURLConnection and enhancing the httpCall to handle the writing of data as well, for POST calls.

Leave a Reply

Your email address will not be published. Required fields are marked *