HTTP Client

The javaxt.http package contains two classes used to represent HTTP Requests and Responses. These classes simplify some of the basic logic used to upload and download content over HTTP while at the same time extending the basic functionality available in the standard Java URLConnection object. Here are some of the highlights.

Downloading Content from a Server

Here's an example of how to download a simple text document using a single line of code:

   String url = "";
   String text = new javaxt.http.Request(url).getResponse().getText();

Here's an example of how to download an XML file:

   String url = "";
   org.w3c.dom.Document rss = new javaxt.http.Request(url).getResponse().getXML();

Here's an example of how to download an image:

   String url = ""; image = new javaxt.http.Request(url).getResponse().getImage();

GZIP Compressed Responses

By default, HTTP requests include "Accept-Encoding: gzip,deflate" in the request header. This allows HTTP servers to return gzip compressed responses. This reduces the amount of data transmitted from the server to the client and thus increasing performance.

Most of the methods in the javaxt.http.Response class automatically decompress GZIP encoded server responses including:

  • getBytes()
  • getText()
  • getImage()
  • getXML()

The one exception is the getInputStream() method. This will return the raw response from the server. Here's an example of how to decompress GZIP encoded data returned from a HTTP server using the getInputStream() method.

   String url = "";
   javaxt.http.Response response = new javaxt.http.Request(url).getResponse(); inputStream = response.getInputStream();

   ByteArrayOutputStream bas = new ByteArrayOutputStream();
   String encoding = response.getHeader("Content-Encoding");
   if (encoding.equalsIgnoreCase("gzip")){

       GZIPInputStream gzipInputStream = null;
       byte[] buf = new byte[1024];
       int len;
           gzipInputStream = new GZIPInputStream(inputStream);
           while ((len = > 0) {
               bas.write(buf, 0, len);
       catch(Exception e){

       try { gzipInputStream.close(); } catch (Exception e){}
       try { bas.close(); } catch (Exception e){}

   try { inputStream.close(); } catch (Exception e){}

Of course, you can set a HTTP request header to "Accept-Encoding: deflate" and the server won't compress the response. Example:

   String url = "";
   javaxt.http.Request request = new javaxt.http.Request(url);
   request.setHeader("Accept-Encoding", "deflate"); inputStream = request.getResponse().getInputStream();

Cache Control and 304 Response Codes

By default, HTTP requests include a no-cache directive. Users can override this behaviour by modifying request headers.

   String url = "";
   javaxt.http.Request request = new javaxt.http.Request(url);
   javaxt.http.Response response = request.getResponse();


   String eTag = response.getHeader("ETag");
   String modDate = response.getHeader("Last-Modified");

   request = new javaxt.http.Request(url);
   request.setHeader("If-None-Match", eTag);
   request.setHeader("If-Unmodified-Since", modDate);