11 Http Java API

Pravin Jain

Introduction

 

In the last module we have seen about the Http Protocol, the format of the Http Protocol, the format of the Http Request, the format of the Http Response. In this module we will be looking at using Http protocol for a Java application. We would like to be writing a client side of Http (Http client) from a Java application.

 

A Java application which should be able to create a Http request, send it to server and receive a response from the server and then, it is not necessary whatever response comes should be rendered, yes, a browser is trained to do this, but as far as a java application is concerned, it could use it for any purpose, it can do the parsing of the content, there can be various uses of whatever comes as a content on the Http protocol. So, fetching content of any URL from within a Java application. So, lets look at how do we do this from a Java applicaiton. Let us explore, here we already know about the Http protocol, So, our idea here is to get the content from any URL, which is a Http URL, normally. It could be other URLs also, but we concentrate more on the http based URLs,

 

So, if we want to fetch any content, we hava several classes which part of a java.net package, which can help us to do this.

 

The java.net package

 

In the java.net package, one of the class which we will need is the class called URL. A URL represents a resource on any host. What is the syntax, and the information available in the URL.

 

The syntax for URL, The URL normally starts with a protocol, for eg. A URL begins with http, so a URL will have protocol:// and then the next part, normally is a hostname. eg.

 

http://host:

 

it it also possible in the URL to include authentication related information, before the host name, who is the user(username) there can be username password that is authentication related information.

 

For example, some username@hostname and if password is required then username:password@hostname. So, in case a resoure access requires authentication, then, this is how the URL may be specified by to include the username and password information in the URL, http://username:password@hostname/resourcepath. In the above case if the password is part of the URL it is fine, otherwise the password may be prompted by the browser. Many times these resources are such, which are nothing but some applications which trigger some kind of a application component to be triggered on the server side to execute some things. Such processes on the server side which receive Http request may also like to have parameters coming to them, some input parameters coming to them and therefore, a URL, a Http URL can also be including after the resourcepath which may have any level of hierarchy, and you specify the filename kind of thing then you can say ? If you would like to have a input parameters being made available to the resource, you could include that by using key value pairs, known as query string.

 

Query string is made up of ? followed by some parameter name = parameter value. If there are multiple parameters, we may have to separate each such key value pairs by an &.

 

The URL class

 

So, what does a URL have, it has a protocol, it can have user information, it can have host, it can have port number, yes, it is optional, by default it is port 80, it could have a path, it could have file at the end and then it can also include a query string, or sometimes it may not have a query string and then in case of html file, you might also include a reference, like # and some reference, eg. index.html#home.So for a http client in Java, first you should create an object of URL. URL class has various constructors available,

 

public URL(String spec)

public URL(URL context, String spec)

public URL(String protocol, String host, int port, String file)

public URL(String protocol, String host, String file)

 

it has various constructor may include protocol, host, port number and path. If you have an existing URL object. It is representing some resource – directory path and within that directory path there is some file and if you want to create another URL object for the file which is in the same directory, then you can do it using existing URL by simply specifying two parameters first is URL and second is specification which is relative to the first parameter URL. So these are the various ways of creating the URL.

 

Our interest is to fetch the content from the internet. Let us say there is some image, the image is available from http:// we may have some path to the jpeg file. Knowing this URL my interest is to be able to downlaod that image. My interest is to send the Http request from the Java application, and get the content. How do I create the request?

 

First part of the request is the url which is to be created. Now, let us say we got our URL created for us, you got an object of URL, now in order to make a request, there is another class which is useful for dealing Http protocol, not only Http protocol even other protocol, which is useful for getting the content, called URLConnection.

 

The URLConnection class

 

How do we get an object of URLConnection? We don’t use a constructor here, the constructor here is protected, you cannot create object of URLConnection by using the constructor. To get the object of URLConnection, on the instance of URL you need to call the method openConnection() having the return type URLConnection.

 

This URLConnection object can then be used for making a Http request and getting back a response. So what does this method do? The method name is openConnection, what do you think is happenning, the method name gives an impression that it has made a connection to the URL we have given, it is not like that. This method does not actually make any connection to the URL. The URL has information about the host and the port, but no connection is done by the openConnection method. The openConnection method is simply making URLConnection object.

 

In the first phase the URLConnection object which we have got has not made any connection at all.   It is the object which has the capabilities of managing information related to request a response both. It is a single object containing information related to the request as well as response part. So, just a creation of such an object is what is happening with the help of method openConnection(). There is no connection, there is no socket creation or any such thing happening in this method. The name may give such an impression but that is not the case here.

 

We have seen earlier in the Http protocol, there is a format of the request, So, we are interested in sending a request to the server and getting a response back. So, now we are interested in setting up the request. To setup the request, We know from the format of the request, there is the method, One more poin8t here, the URLConnection here is slightly general here, it is not used only for http protocol, it is also useful for other protocols, for eg. It could also be used for the ftp protocol to fetch a resource from ftp server, we use ftp protocol, so, there are certain things specific to http protocol, you will not find them in the URLConnection class.

 

URLConnection has a sub-class called HttpURLConnection, it is here that you will find thngs which are more specific to Http protocol. URLConnection is simply a general purpose class which is meant for anything, any kind of protocol which is having exchange of content, where content is going to come from a server, where we can send a request and some kind of content. As a client I fetching some kind of a content from a URL.

 

The HttpURLConnection class

 

When you use the openConnection method, if the URL starts with http, in that case the URLConnection object which you get is not actually of URLConnection but it is of HttpURLConnection. The URLConnection class is actually an abstract class. HttpURLConnection is actually the class, whose object will be creaed. So, when you use openConnection() to get object of URLConnection, then, knowing that the protocol specified is http, you should be able to cast it into HttpURLConnection, and use the methods of HttpURLConnection.

 

To setup the request to be sent to the server, what were the fields, if we look at Http protocol, the first field was a method. So we have this method called setRequestMethod by which you can specify the method you desire to use. By default, when you get the HttpURLConnection object using openConnection method, it would be having GET as the default method, So, if you want to change the method as POST, you want to be posting some parameters to the server, in such case this can be used for changing the method. The openConnection has not actually made the connection or sending any kind of request, it is simply giving us an object where you are now setting up the whole request. 

 

So looking at the first line of the Http Request, we have the method, which can be set using the setRequestMethod, method. The second part is the resource. From the URL the resource has been picked up so that is already decided by the URL instance. The third part is the protocol version, By default the protocol version will be 1.1 and you cant change that.

 

Then we have the headers, you may be interested in setting up the headers in the request. There are several headers and we have a method called setRequestProperty to setup the headers. Headers are key value pairs. So, this method can be used to adding or settingup headers in the request.

 

After the openConnection you are in a phase of setting up the request. So, we can setup the request, when does it actually make the connection?

 

For making a connection, in the URLConnection class we have a method called connect(), it is then the request is sent to the server and getting the response back from the server. And whatever information is received from the server is also collected in the same URLConnection Object. So URLConnection object is capable of storing information, which is related to the request, as well as the response part.

 

It has various states:

 

1)  Request setup state

2)  Connected state

 

Initially it is in the request setup state. Then it would be in the connected state. It is making connection and then someone might even close it. The connect method would make a connection, once it has make a connection, it will send request, receive response and populate the information received from the response in the object.

 

In case of POST method, we should be able to add some content into it. In URLConnection we have the method getOutputStream it gives us the outputstream which allows to write the content into the URL. But this is to be allowed in the request setup state.

 

Once the request is setup, we can call the connect method, send the request and receive the respone, using the connect method.

 

So, in the response we have status to know what has happened to the response. So, to know that we have a method called getResponseCode(). It returns an int value.

 

We can have method to get the headers of the response. In case I call the method getResponseCode in the request setup state will it give me error? Answer is No.

 

Whenever we use any method related to response, then URLConnection will first do a check that what is my state? have i obtained the response. If not and someone is asking for the information related to the response then I need to check the state of the request whatever is the current state of the request send the request receive the response and then return the information he is asking for.

 

So we dont need to call the connect method explicitly always. Using any method which relates to the response, will do the job for us, whatever the connect was doing for us.

 

Now I would like to read the content and for doing that we have a method called getInputStream. It gives us the InputStream which allows us to read the content from the response content part. I can simply use the URL class itself, if there is no change i want to make on the request part and i am OK using the default request and only concern in receiving the content of the response then URL class also has the getContent method.

 

Exercise: Check for the URL to get some google image and then get the URLConnection object using the URL as a string. open the input stream and write the code which written on this InputStream and write it down in some file.

 

Suggested Reading:

 

1. Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.
2. Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
3. https://docs.oracle.com/javase/tutorial/networking/urls