Home » Core Java » apache » ANT » Apache Ant HTTP Get Example

About Simranjit Singh

Simranjit Singh has graduated from Computer Science Department of the Guru Nanak Dev University of Amritsar, Punjab, India. He also holds a Master degree in Software Engineering from the Birla Institute of Technology & Science of Pilani, Rajasthan, India. He works as a Senior Consultant in the e-commerce sector where he is mainly involved with projects based on Java and Big Data technologies.

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.

ant get

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:

  1. Ability to name the remote URL which is the target of the request.
  2. Ability to name a local file as the local store of any returned content.
  3. Ability to name property as the local store of any returned content.
  4. Ability to name a property to be set to “true” when a request succeeds.
  5. 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.
  6. 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
  7. A ‘verbose’ option which provides extra information and progress information during a download.
  8. 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.

AttributeDescriptionRequired
srcthe URL from which to retrieve a file.Yes
destthe file where to store the retrieved file.Yes
verboseshow verbose progress information (“on”/”off”).No; default “false”
ignoreerrorsLog errors but don’t treat as fatal.No; default “false”
usetimestampconditionally download a file based on the timestamp of the local copy. HTTP onlyNo; default “false”
usernameusername for ‘BASIC’ HTTP authenticationif password is set
passwordpassword: requiredif 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.

Download
You can download the full source code of this example here: Apache Ant HTTP Get Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments