Apache Ant HTTP Get Example
In this example, we will explain Apache Ant HTTP Get action.
1. Introduction
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other.
The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing them to compile, assemble, test, and run Java applications. Ant can also be used effectively to build non-Java applications, for instance, C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.
Ant uses an XML file for its configuration. The default file name is build.xml. Ant builds are based on three blocks: tasks, targets, and extension points.
A task is a unit of work which should be performed and constitutes of small atomic steps, for example, compile source code or create Javadoc. Tasks can be grouped into targets.
A target can be directly invoked via Ant. Targets can specify their dependencies. Ant will automatically execute all dependent targets.
2. Http Antlib
This antlib contains tasks to make the basic HTTP requests: get, post, head, put, with Basic authentication.
Common functionality to the core tasks tasks is:
- Ability to name the remote URL which is the target of the request.
- Ability to name a local file as the local store of any returned content.
- Ability to name property as the local store of any returned content.
- Ability to name a property to be set to “true” when a request succeeds.
- The option to list a number of parameters, each with a name and a value. Some methods (HttpGet, HttpHead) attach these parameters to the stated URL to generate the URL to actually fetch. Others (HttpPost) send the parameters up in the standard representation of form data.
- The option to state the authentication policy and then the username and password. Currently, only basic authentication is used, which is utterly insecure except over an https link
- A ‘verbose’ option which provides extra information and progress information during a download.
- Timestamp control, using the usetimestamp flag. When setting the timestamp of downloaded content is set to match that of the remote file and the local timestamp of the destination file (if it exists) used to set the if-modified-since header in the request, which will trigger optional download only.
3. HttpGet
Accesses a URL to retrieve a file or to cause some action on the server.
All the attributes of the HTTP task may be used. Note that a quirk of the implementation of the HTTP client in java makes it impossible to reliably fetch the response details from any unsuccessful request against a URL that doesn’t end in ‘.htm,.html or .txt’. This means that if the task is used to compile JSP pages by issuing a request against them, the text details of any errors will not be picked up.
3.1 Examples
<httpget url="http://jakarta.apache.org/" destFile="help/index.html"/>
Fetches the index page of http://jakarta.apache.org/, and stores it in the file help/index.html.
<httpget src="http://jakarta.apache.org/builds/tomcat/nightly/ant.zip" destFile="optional.jar" verbose="true" usetimestamp="true" > <header name="Cookie" value="someid=43ff2b"/> </httpget>
Retrieves the nightly ant build from the tomcat distribution, if the local copy is missing or out of date. Uses the verbose option for progress information. A cookie is supplied for the server’s benefit.
<httpget url="https://www.pizzaservices.com/prices.jsp" destFile="pizza-prices.xml"> <param name="zipcode">57340</param> <param name="pizza">pepperoni</param> </httpget>
Builds a URL by adding parameters (“?zipcode=57340&pizza=pepperoni”) to the base URL and then fetches the contents (fictional example)
The following is a basic example of how to import and use the missing link Ant HTTP task:
<?xml version="1.0" encoding="UTF-8"?> <project name="ml-ant-http" basedir="." default="http-get"> <property name="ml-ant-http.jar" value="ml-ant-http-1.0.jar"/> <fileset id="runtime.libs" dir="."> <include name="${ml-ant-http.jar}"/> </fileset> <path id="runtime.classpath"> <fileset refid="runtime.libs"/> </path> <taskdef name="http" classname="org.missinglink.ant.task.http.HttpClientTask"> <classpath refid="runtime.classpath"/> </taskdef> <target name="http-get"> <http url="http://www.google.com"/> </target> </project>
4. Parameters
The different parameters options available for the httpget task.
Attribute | Description | Required |
src | the URL from which to retrieve a file. | Yes |
dest | the file where to store the retrieved file. | Yes |
verbose | show verbose progress information (“on”/”off”). | No; default “false” |
ignoreerrors | Log errors but don’t treat as fatal. | No; default “false” |
usetimestamp | conditionally download a file based on the timestamp of the local copy. HTTP only | No; default “false” |
username | username for ‘BASIC’ HTTP authentication | if password is set |
password | password: required | if password is set |
5. Apache Ant HTTP Get – Summary
Here in this Apache Ant HTTP Get Example, we have learned about the different ways on how to implement HTTP Get task.
You can learn more about Apache ant Java tool by reading our tutorials.
6. Download the Eclipse Project
This was an example of using Apache Ant with Java.
You can download the full source code of this example here: Apache Ant HTTP Get Example