Tuesday, February 21, 2017

Using Apache HttpClient Fluent API

The Apache HttpClient provides reach functionality and flexibility for implementing of HTTP client java applications.
The Fluent API is an easy facade to the basic HttpClient functionality. It may be quite sufficient for simple applications, which should not deal with connection management, resources allocation or other advance stuff. The fluent API concept is designed to call methods in chain, which makes code rather compact and intuitive.

The Executor style exists for reason. It helps in cases demanding more control on security or other settings, which cannot be specified with the Request. We will consider such example later on in this post.

Adding ResponseHandler

Fluent API implements buffering of the response message in memory. That is why it is strongly recommended to execute fluent HTTP requests with providing the ResponseHandler. The ResponseHandler interface represents a handler, which processes a raw HttpResponse and converts it to an application response object.

Let's see an example.
1. The class AppResponse encapsulates an application response:

2 comments
:

You have been able to demonstrate in the context of web development the most convenient features for optimization. It seems that it is, it is possible to get a request without a headache. Previously, I had to deal with HtmlUnit, but after reading the Java tutorials explainjava.com I understood a lot of additional things. In the case of the obvious experience and the difference is that the functional HttpClient really gives more flexibility and reduces the test time. Your article is useful and the step-by-step instruction helped me.

About the author

I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.