HTTPClient 0.3-3 review

Download
by rbytes.net on

This package provides a complete http client library

License: LGPL (GNU Lesser General Public License)
File size: 538K
Developer: Ronald Tschal?r
0 stars award from rbytes.net

This package provides a complete http client library. It currently implements most of the relevant parts of the HTTP/1.0 and HTTP/1.1 protocols, including the request methods HEAD, GET, POST and PUT, and automatic handling of authorization, redirection requests, and cookies.

Furthermore the included Codecs class contains coders and decoders for the base64, quoted-printable, URL-encoding, chunked and the multipart/form-data encodings. The whole thing is free, and licenced under the GNU Lesser General Public License (LGPL) (note that this is not the same as the GPL).

Following are the kits and documentation for the HTTPClient Version 0.3-3. If you have any problems, bugs, suggestions, comments, etc. see the info on debugging and reporting problems. An older version of these pages are also available in Japanese, thanks to the kindly efforts of Yuji Kumasaka.

Using the HTTPClient should be quite simple. First add the import statement import HTTPClient.*; to your file(s). Next you create an instance of HTTPConnection (you'll need one for every server you wish to talk to). Requests can then be sent using one of the methods Head(), Get(), Post(), etc in HTTPConnection.

These methods all return an instance of HTTPResponse which has methods for accessing the response headers (getHeader(), getHeaderAsInt(), etc), various response info (getStatusCode(), getReasonLine(), etc), the response data (getData(), getText(), and getInputStream()) and any trailers that might have been sent (getTrailer(), getTrailerAsInt(), etc). Following are some examples to get started.
To retrieve files from the URL "http://www.myaddr.net/my/file" you can use something like the following:
try
{
HTTPConnection con = new HTTPConnection("www.myaddr.net");
HTTPResponse rsp = con.Get("/my/file");
if (rsp.getStatusCode() >= 300)
{
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
data = rsp.getData();

rsp = con.Get("/another_file");
if (rsp.getStatusCode() >= 300)
{
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
other_data = rsp.getData();
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
}
catch (ParseException pe)
{
System.err.println("Error parsing Content-Type: " + pe.toString());
}
catch (ModuleException me)
{
System.err.println("Error handling request: " + me.getMessage());
}

This will get the files "/my/file" and "/another_file" and put their contents into byte[]'s accessible via getData(). Note that you need to only create a new HTTPConnection when sending a request to a new server (different protocol, host or port); although you may create a new HTTPConnection for every request to the same server this not recommended, as various information about the server is cached after the first request (to optimize subsequent requests) and persistent connections are used whenever possible (see also Advanced Info).

To POST form data from an applet back to your server you could use something like this (assuming you have two fields called name and e-mail, whose contents are stored in the variables name and email):
try
{
NVPair form_data[] = new NVPair[2];
form_data[0] = new NVPair("name", name);
form_data[1] = new NVPair("e-mail", email);

// note the convenience constructor for applets
HTTPConnection con = new HTTPConnection(this);
HTTPResponse rsp = con.Post("/cgi-bin/my_script", form_data);
if (rsp.getStatusCode() >= 300)
{
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
stream = rsp.getInputStream();
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
}
catch (ModuleException me)
{
System.err.println("Error handling request: " + me.getMessage());
}

Here the response data is read at leisure via an InputStream instead of all at once into a byte[].

As another example, if you want to upload a document to a URL (and the server supports http PUT) you could do something like the following:
try
{
URL url = new URL("http://www.mydomain.us/test/my_file");

HTTPConnection con = new HTTPConnection(url);
HTTPResponse rsp = con.Put(url.getFile(), "Hello World");
if (rsp.getStatusCode() >= 300)
{
System.err.println("Received Error: "+rsp.getReasonLine());
System.err.println(rsp.getText());
}
else
text = rsp.getText();
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
}
catch (ModuleException me)
{
System.err.println("Error handling request: " + me.getMessage());
}

HTTPClient 0.3-3 keywords