JavaXT

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 = "http://www.swig.org/Release/LICENSE";
   String text = new javaxt.http.Request(url).getResponse().getText();

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

   String url = "http://news.google.com/news?ned=us&topic=h&output=rss";
   org.w3c.dom.Document rss = new javaxt.http.Request(url).getResponse().getXML();

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

   String url = "http://www.google.com/images/logos/ps_logo2.png";
   javaxt.io.Image image = new javaxt.http.Request(url).getResponse().getImage();
   image.saveAs("/google.png");

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 = "http://www.google.com/";
   javaxt.http.Response response = new javaxt.http.Request(url).getResponse();
   java.io.InputStream 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;
       try{
           gzipInputStream = new GZIPInputStream(inputStream);
           while ((len = gzipInputStream.read(buf)) > 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 = "http://www.swig.org/Release/LICENSE";
   javaxt.http.Request request = new javaxt.http.Request(url);
   request.setHeader("Accept-Encoding", "deflate");
   java.io.InputStream inputStream = request.getResponse().getInputStream();
   new javaxt.io.File("/temp/license.txt").write(inputStream);
   inputStream.close();

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 = "http://www.kartographia.com/warning.png";
   javaxt.http.Request request = new javaxt.http.Request(url);
   javaxt.http.Response response = request.getResponse();

   System.out.println(request);
   System.out.println(response);

   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);