You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Ross Rankin <ro...@bellsouth.net> on 2003/03/02 17:21:16 UTC

Proper Order of things...

I am getting inconsistent results on a few things and I realized I really am
sort of parroting examples and really don't know the true order of things
when doing a series of connections.  So let me ask a few questions:

 

 1) If doing to of the same type of requests what is the correct method for
they type of action do I:

 

a)       use two Method variables

b)       use the same one with a recycle but cast it new again

c)       use the same one without a recycle but cast it new

d)       use the same one with a recycle but use setPath between executes

e)       none of the above

 

2) If doing a Post which returns another page do I need to really Get that
page?

 

3) What the proper place for a method.releaseConnection? After the method,
after you are all done?

 

4) Once the client is set up and configured, do you need to do anything to
maintain it?  

 

5) Rejected cookies.  OK so the system I'm connecting to can not make a good
cookie, can't I accept it anyway?  

 

6) Starting out, here the order I think is correct am I right?

            a) create a host configuration

            a) create a connection using that host config

            b) use that connection to create a connection Manager

            c) use that connection Manager to create a client

            d) create a Method

            e) execute that method using the host config and client

 

 


Re: Proper Order of things...

Posted by Laura Werner <la...@lwerner.org>.
Ross Rankin wrote:

>I am getting inconsistent results on a few things and I realized I really am
>sort of parroting examples and really don't know the true order of things
>when doing a series of connections.  So let me ask a few questions:
>
What sorts of inconsistent results?

> 1) If doing to of the same type of requests what is the correct method for
>they type of action do I:
>
>a)       use two Method variables
>
This works.

>b)       use the same one with a recycle but cast it new again
>
What do you mean by "cast it new again"?  Aside from that, this works; 
you can re-use HttpMethodMethod objects as long as you call recycle() 
first.  After you call recycle(), the object is essentially "virgin" 
again, so you have to call setPath, re-set all of the headers, and so on.

>c)       use the same one without a recycle but cast it new
>
This won't work.  You must call recycle() if you're going to re-use a 
HttpMethod object.

>d)       use the same one with a recycle but use setPath between executes
>
See the answer to b).

>2) If doing a Post which returns another page do I need to really Get that
>page?
>
No, if I understand the question correctly.  If you're writing a 
browser-type application and the POST is a page-transition sort of POST, 
then the data for the new page/resource will be returned in the response 
to the POST. There's no need to follow it by a GET.  Of course, someone 
*could* dream up a protocol layered on top of HTTP that required all 
POSTs to be followed by GETs, but I've never heard of one.

>3) What the proper place for a method.releaseConnection? After the method,
>after you are all done?
>
After you're finished reading from the method's input stream, I think.  
I'm not 100% sure about this one though, because this part of httpclient 
has changed a lot.

>4) Once the client is set up and configured, do you need to do anything to
>maintain it?  
>
HttpClient isn't really a server that you "set up" or "configure".  It's 
just a class library that you call.  So I don't really understand the 
question.

>5) Rejected cookies.  OK so the system I'm connecting to can not make a good
>cookie, can't I accept it anyway?  
>
You can experiment with the "apache.commons.httpclient.cookiespec" 
system property.  By default, HttpClient uses the RFC2109 cookie policy, 
which is fairly strict.  If you set this property to "COMPATIBILITY", it 
will be more lenient and might accept the broken cookies from your 
server.  See the code in 
org.apache.commons.httpclient.cookie.CookiePolicy for details.

>6) Starting out, here the order I think is correct am I right?
>            a) create a host configuration
>            a) create a connection using that host config
>            b) use that connection to create a connection Manager
>            c) use that connection Manager to create a client
>            d) create a Method
>            e) execute that method using the host config and client
>  
>
Close.  You don't need to create a connection yourself.  Just create a 
connection manager (probably a MultiThreadedHttpConnectionManager) and 
then use that to create an HttpClient.  The connection manager will 
create the connections itself, as needed.  Then you create methods and 
execute them using your host config and client.

Laura Werner

Re: Proper Order of things...

Posted by Michael Becke <be...@u.washington.edu>.
Ross,

Below are some answers to your questions.

>  1) If doing to of the same type of requests what is the correct 
> method for
> they type of action do I:
> a)       use two Method variables
>
> b)       use the same one with a recycle but cast it new again
>
> c)       use the same one without a recycle but cast it new
>
> d)       use the same one with a recycle but use setPath between 
> executes
>
> e)       none of the above

There are many options.  It depends what specifically your needs are.  
I generally only use a method once.  The only real restrictions are as 
follows:

1 - methods are not thread safe so they can only be used by one thread 
at a time
2 - a method must be recycled before it can be reused

>
> 2) If doing a Post which returns another page do I need to really Get 
> that
> page?

I'm not exactly sure what your asking, but, after you do a post, the 
server's response can be read using one of the getResponseBody*() 
methods.

> 3) What the proper place for a method.releaseConnection? After the 
> method,
> after you are all done?

The connection should be released after it is no longer being used.  A 
connection is given to a method each time it is executed.  So, the 
connection is no longer being used after a particular method execution 
and the corresponding response are completed.  It will most likely look 
something like:

try {
	client.executeMethod(method);
	// process method response here
} catch (Exception e ) {
	// do something
} finally {
	method.releaseConnection();
}


> 4) Once the client is set up and configured, do you need to do 
> anything to
> maintain it?

I can't think of anything.  I'm not sure what you mean by maintain 
though.

>
> 5) Rejected cookies.  OK so the system I'm connecting to can not make 
> a good
> cookie, can't I accept it anyway?

It is possible to change the cookie parsing policy to be more lenient.  
Try:

CookiePolicy.setDefaultPolicy(CookiePolicy.COMPATABILITY);

> 6) Starting out, here the order I think is correct am I right?
>
>             a) create a host configuration
>
>             a) create a connection using that host config
>
>             b) use that connection to create a connection Manager
>
>             c) use that connection Manager to create a client
>
>             d) create a Method
>
>             e) execute that method using the host config and client

It should look something like:

	HttpClient httpClient = new HttpClient();
	httpClient.getHostConfiguration().setHost("jakarta.apache.org", -1, 
"http");
	GetMethod method = new GetMethod("/");
	httpClient.executeMethod(method);
	
Please take a look at the examples directory for some more ideas of how 
HttpClient can be used.  The examples dir is included in the source and 
is also available at:

   http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/src/examples/


Mike