commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Waldhoff, Rodney" <>
Subject RE: HttpClient Usage
Date Tue, 29 Jan 2002 23:47:47 GMT
If you want to submit an XML document as the body, I believe what you really
want is a PUT not POST method, but you could really do either.

There are a number of examples in the /src/test tree (see
/commons/httpclient/), but here's a snippet for PUT:

  HttpClient client = new HttpClient();
  client.startSession(host, port);
  PutMethod method = new PutMethod("/foo/bar");
  method.setRequestBody("This is data to be sent in the body of an HTTP

I'm pretty sure that the Content-Length value will be calculated
automatically (or chunked output stream will be used).

To POST an arbitrary body (as opposed to a collection of name/value pairs),
I think you've got a few options:

A) use PostMethod directly, and add a single parameter containing the body
as the name, but no value (e.g., method.setParameter(myXmlDocument,null))

B) extend PutMethod such that getName() returns POST  (we could add a
setName method to HttpMethodBase if this is generally useful)

C) extend PostMethod and override writeRequestBody to write your document as

-----Original Message-----
From: Glenn Kidd []
Sent: Tuesday, January 29, 2002 5:08 PM
To: ''
Subject: HttpClient Usage

Are there any examples out there for using HttpClient to POST?  I looked
around and I could not find any using the newer versions of HttpClient.  I
need to make a Client that can submit a POST with an XML document as the
body.  As such I need the Content-Length header to get set as well.  Can
anyone point me in the right direction or send a code snippet.  Any help
would be much appreciated.  


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message