You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by sreenivas somavarapu <so...@gmail.com> on 2023/02/10 16:35:51 UTC

AbstractClassicEntityConsumer and AbstractClassicEntityProducer API usage and chunked encoding in request

Hi Team,



We are currently trying to implement Apache HTTP Async Client API for
handling HTTP 1.1 and HTTP 2.0 request (Using Servlet request and
response). We have implemented classes using AbstractClassicEntityConsumer
and AbstractClassicEntityProducer which generates request / consumes
response into byte array. This results in all request being sent to web
server (IIS / Apache) using *chuncked encoding* eventhough we don’t
explicitly set it in request headers. Is this expected? Also we are seeing
another issue wherein when using POST request the data in body is not sent
to backend server which is causing in loss of the data. Is this behavior
due to usage of byte array of response data. Tried to replace byte array in
consumeData with different options like returning InputStream etc., but
execute method is showing error. Is this usage valid or any other way to
achieve POST using Async using Servlet streams (Request and Response).



*Sample of what being defined in produceData and consumeData abstract
methods are below*

    @Override

    protected void *produceData*(ContentType contentType, OutputStream
outputStream) throws IOException {

        if (content != null) {

            byte[] buf = new byte[4096];

            int length;

            while ((length = content.read(buf)) != -1) {

                outputStream.write(buf, 0, length);

            }

        } else {

            throw new IllegalStateException("Content must be set before
entity is written");

        }

    }



    @Override

    protected byte[] *consumeData*(ContentType contentType, InputStream
instream) throws IOException {

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        byte[] data = new byte[4096];

        int read;

        while ((read = instream.read(data, 0, data.length)) != -1) {

            buffer.write(data, 0, read);

        }



        buffer.flush();

        return buffer.toByteArray();

    }



*Our HTTPClient method execute looks something below*

            Future<Message<HttpResponse, byte[]>> responseHandler =
httpAsyncClient.execute(reqBuilder.build(),

                    new BasicResponseConsumer<>(new
AsyncResponseEntity<byte[]>()), null, HttpClientContext.create(), null);



*AsyncRequestBuilder setEntity* method is set with new object created using
implementation class extending *AbstractClassicEntityProducer*



*HTTPClient log extract*

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> connection: Keep-Alive

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> Accept-Language: en-US

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> Accept-Encoding: gzip, deflate

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> user-agent: Mozilla/5.0 (Windows NT 6.1; rv:38.0)
Gecko/20100101 Firefox/38.0

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> accept: */*

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> Transfer-Encoding: chunked

2023-02-08 10:52:46,728 DEBUG [org.apache.hc.client5.http.headers]
c-0000000000 >> Host: server



--
Regards,
S. Sreenivas