hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Forcing Connection: Close
Date Thu, 01 Dec 2011 20:43:53 GMT
On Thu, 2011-12-01 at 13:11 -0500, William Speirs wrote:
> I'm trying to make a simple HTTP/1.0 request where the Connection header is
> set to Close. I have the following basic code:
> 
> HttpParams params = new SyncBasicHttpParams();
> // set the version to 1.0
> params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
> HttpVersion.HTTP_1_0.toString());
> httpclient = new DefaultHttpClient(params);
> 
> // I attempt to force Connection: Close here
> httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
>     public long getKeepAliveDuration(HttpResponse response, HttpContext
> context) {
>         return 0;
>     }
> });
> 
> BasicHttpEntityEnclosingRequest request = new
> BasicHttpEntityEnclosingRequest("POST", baseUrl, HttpVersion.HTTP_1_0);
> 
> HttpResponse response = httpclient.execute(httpHost, request);
> 
> 
> A few questions:
> 
> 1) Why doesn't setKeepAliveStrategy() for the client seem to actually do
> that... set the keep alive strategy?
> 

The keep-alive strategy determines how long a connection can be kept
alive while idle based on the properties of a response object

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/ConnectionKeepAliveStrategy.html

It does not populate any HTTP headers. That is the responsibility of
protocol interceptors.

> 2) Why do I have to set the version of the request a second time: once with
> the client via a parameter, and again with the request. Setting the
> parameter value in the client seems to have no affect.
> 

The parameter applies only when a protocol version is not set
explicitly, for instance, like that

HttpGet httpget = new HttpGet("/");

> 3) Finally, how do I force Connection: Close instead of Connection:
> Keep-Alive?
> 

You can either set the header manually or use a protocol interceptor to
set it automatically on all incoming or outgoing messages.

HttpGet httpget = new HttpGet("/");
httpget.setHeader("connection", "close");

Hope this helps

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message