Chapter 14

Using HTTP and REST

HTTP is one of the core ways for requesting and receiving data over the Internet. It is used for communication over the World Wide Web. HTTP servers, such as Apache’s HTTPD, Microsoft’s IIS, and Tomcat, are configured to listen for structured incoming requests, and respond appropriately.

The HTTP Methods

What is HTTP?

A good exercise in understanding how the HTTP protocol works is to perform an HTTP request outside of a browser using an application such as Telnet which is provided with most modern operating systems.

To make a request to a site, such as Wikipedia, open a Telnet connection to port 80. You should see output similar to Listing 14-1.

Listing 14-1: Initiating an HTTP request using Telnet

> telnet en.wikipedia.org 80
Trying 91.198.174.225...
Connected to wikipedia-lb.esams.wikimedia.org.
Escape character is '^]'.

Your command prompt should be on a blank new line. The server hosting your request is now waiting for you to make your request for a certain page. Type GET / HTTP/1.1, followed by new lines. Your Telnet session should respond with a set of HTTP headers, followed by some HTML, and will then close the connection and take you back to the command line, like in Listing 14-2.

Listing 14-2: Creating an HTTP request using Telnet

GET / HTTP/1.1 HTTP/1.0 200 OK Server: Apache X-Content-Type-Options: nosniff Cache-Control: s-maxage=3600, must-revalidate, max-age=0 Last-Modified: Mon, 13 May 2013 17:07:15 GMT Vary: Accept-Encoding Content-Length: ...

Get Java Programming Interviews Exposed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.