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: upgrading from v3 to v4, setting request headers
Date Thu, 29 Aug 2013 07:55:22 GMT
On Wed, 2013-08-28 at 08:55 -0700, Mike Wertheim wrote:
> I'm upgrading from commons-httpclient-3.1 to httpclient-4.2.5
> and fluent-hc-4.2.5.
> 
> My code is accessing a web services API that requires some arguments be
> passed in as http request headers.
> 
> The version of my code that uses commons-httpclient-3.1, which definitely
> works, is this:
> HttpClient httpClient = new HttpClient();
> GetMethod getMethod = new GetMethod(urlString);
> getMethod.setRequestHeader("search-client-ip", ipAddressString);
> getMethod.setRequestHeader("User-Agent", userAgentString);
> httpClient.executeMethod(getMethod);
> InputStream is = getMethod.getResponseBodyAsStream();
> 
> The version of my code that uses httpclient-4.2.5 and fluent-hc-4.2.5 is
> this:
> Request request = Request.Get(urlString);
> request.addHeader("search-client-ip", ipAddressString);
> request.addHeader("User-Agent", userAgentString);
> InputStream is =
> request.execute().returnResponse().getEntity().getContent();
> 
> The new version doesn't seem to work as well as the old version.  I haven't
> been able to determine exactly what the issue is, other than that
> occasionally no data is returned.
> 

Hi Mike

I think these two code snippets are pretty much compatible and should
produce compatible HTTP requests.

You probably want to run both versions of your code with wire / context
logging on and examine the difference between sessions (successful and
unsuccessful ones, as well as old (3.x) and new (4.x) ones)

> If you know both the 3.1 and 4.2.5 APIs pretty well, does the new code look
> correct to you?  I'm wondering if I'm missing something obvious.  Does
> 4.2.5's Request.addHeader behave identically to 3.1's
> GetMethod.setRequestHeader?
> 

For an empty message it should behave identically.

Just a remark. Please note that for simple use cases HC fluent usually
buffers response content in memory in order to simplify resource
management. This may be suboptimal for use in production. So, you might
want to use ResponseHandler to avoid intermediate buffering.

Oleg

> Thanks,
> Mike




---------------------------------------------------------------------
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