hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rodney Waldhoff <rwaldh...@apache.org>
Subject Re: Let's face it: HttpClient API is more complex that it could have been
Date Thu, 12 Dec 2002 15:58:36 GMT
I like this idea a lot, and would argue it is better to make this sort of
change before the 2.0 final release than after (backwards compatiablity to
any previous release is already broken, so lets make the API as right as
we can rather than as similiar as we can).  I think this opens up a
number of interesting possiblities.

One little tweak to suggest:  Currently it is possible to use HttpMethod
instances in isolation.  The HttpClient class provides a context (and in
some ways a simplier API) for executing these methods.  I think this is a
useful feature in a number of ways (among others, it makes it much easier
to create custom clients).  I'd like to retain this feature even under an
HttpRequest/HttpResponse split, which may mean treating HttpRequest as
something of a factory for Responses, for example, something like:

 HttpRequest get = new HttpGetRequest("/index.html");
 HttpResponse response = get.execute(myHttpState, myHttpConnection);

or I suppose even:

 HttpRequest get = new HttpGetRequest("/index.html");
 HttpResponse response = RequestExecutor.execute(

if we wanted to tease apart request execution from the Response and
Request containers.

This tweak doesn't actually contradict any of the example Oleg provides
either, its more of an extension.

One of the "interesting possiblities" I think this opens up is something
I've been wanting to see in HttpClient for some time--to make the
HttpRequest stateless enough that I can easily create "scripts" of
Requests that I can execute over and over.  For example, something like:

 HttpRequest[] script = ....
 for(int i=0;i<script.length;i++) {
   HttpResponse response = script[i].execute(...);

that I could multiple times, perhaps in parallel from multiple threads or
against multiple hosts, etc.  This could be very convienient for
implementing robots or load-testing tools.

On Thu, 12 Dec 2002, Kalnichevski, Oleg wrote:

> It's not the best time to discuss API changes at this point of time when the focus should
be put on polishing the existing code. However, I can't help thinking that the HttpClient
API could have been a bit more straight-forward. Quite a few of the recent postings on this
mailing list were made by people having problems figuring out HttpClient's modus operandi.
Lack of documentation is partially to blame, but complex API does not make things easier either.
I was sort of thinking of an easy way to explain how to get started with the HttpClient. Currently
it almost takes a PhD Stanford to get started with.
> Ideally I would envisage the following pattern of use for the HttpClient
> HttpClient client = new HttpClient();
> HttpResponse response1 = client.execute(new HttpGetMethod("http://host1.ddd.com/index.html"));
> int result1 = response1.getStatusCode();
> Header[] headers1 = response1.getHeaders();
> HttpRequest get = new HttpGetRequest();
> get.setUrl("https://securehost.ddd.com/getstuff")
> get.addQueryParam("param1", "stuff");
> get.addQueryParam("param2", "stuff");
> HttpResponse response2 = client.execute(get);
> int result2 = response1.getStatusCode();
> byte[] body2 = response1.getBody();
> HttpRequest post = new HttpPostRequest();
> post.setUrl("https://securehost.ddd.com/poststuff")
> post.addParam("param1", "stuff");
> post.addParam("param2", "stuff");
> HttpResponse response3 = client.execute(post);
> int result3 = response3.getStatusCode();
> String body3 = response3.getBodyAsString();
> Bottom line:
> - ideally HttpMethod interface should be split into HttpRequest/HttpResponse pair.
> - HttpClient.execute() should always work with full URLs. I feel that client.getHostConfiguration().setHost(host,
port); client.execute(path) sequence is unnecessarily complex.
> - The purpose of HttpMethod.recycle() may not be obvious to some
> - We should think of a browser as a paradigm for HttpClient's "modus operandi": start
the damn thing, hit that url, get response, move to the next one
> Now everyone is welcome to start throwing bad tomatoes at me ;-)))
> Oleg

View raw message