How to read response using HttpsUrlconnection when transfer encoding is chunked

user3565529

I am writing a servlet which internally uses HttpsUrlConnection to call another Url. From the servlet, I have to return the same response and same response headers that I receive from the HttpsUrlConnection call.

For that, I was reading all the headers using getHeaderFields(): copy the headers returned to the Httpservletresponse object of my servlet.

Also, After that I am trying to read response after that using connection.getErrorStream() (this is mostly happening when server returns status > 400), thats why I use getErrorStream. Then I read the bytes and copy the bytes to HttpServletResponse outputstream of my servlet.

Now this is working fine is some cases.

But when the server that I call using the connection returns response with Transfer Encoding chunked, then when I call my servlet API, it is returning an error for invalid chunk encoding if I copy the response and response headers to the Httpservlet response.

In this case, in the logs I see that when I read the headers using getHeaderFields, the first header that i see is Transfer encoding chunked, then the next header name is null and it has value HTTP 1/1 .

Also, the response I see is also not proper.

How to read the response headers and the response body in this case from the httpsurlconnection and return that from my servlet properly?

JimHawkins

I faced a similar issue with chunked encoding. The difference to you is that my servlet used HTTP instead of HTTPS internally.

I use a method to copy the header fields, too. But I supply a list of headers that are managed by the servlet container, so I don't create, copy or edit them:

 private static final Set forbiddenCopyHeaders = new HashSet<>(Arrays.asList(new String[]{
            "connection"
            , "transfer-encoding"
            , "content-length"
            , "via"
            , "x-forwarded-for"
            , "x-forwarded-host"
            , "x-forwarded-server"
    }));

These are the methods I use for copying the response headers:

  private void copyResponseHeaders(CloseableHttpResponse internResponse, HttpServletResponse response)
    {
        Header[] headers = internResponse.getAllHeaders();
        Header[] connHeaders = internResponse.getHeaders("connection");
        StringBuilder connectionValue = new StringBuilder();

        for (Header connHeader : connHeaders)
        {
            connectionValue.append(connHeader.getValue()).append(", ");
        }

        for (Header header : headers)
        {
            String headerName = header.getName();

            boolean copyAllowed = !forbiddenCopyHeaders.contains(headerName.toLowerCase())
                    && !StringUtils.containsIgnoreCase(connectionValue.toString(), headerName);

            if (copyAllowed)
            {
                if (response.containsHeader(headerName))
                {
                    response.addHeader(headerName, header.getValue());
                }
                else
                {
                    response.setHeader(headerName, header.getValue());
                }
            }
        }

        setViaHeader(internResponse, response);
    }

Method setViaHeader():

private void setViaHeader(CloseableHttpResponse response, HttpServletResponse customerResponse)
    {
        String serverHostName = "companyServer";
        try
        {
            serverHostName = InetAddress.getLocalHost().getHostName();
        }
        catch (UnknownHostException e)
        {
            logger.error("für den VIA-Header kann der Hostname nicht ermittelt werden", e);
            System.err.println("für den VIA-Header kann der Hostname nicht ermittelt werden: " +
                    ExceptionUtils.getStackTrace(e));
        }

        Header[] originalViaHeaders = response.getHeaders("via");
        StringBuilder via = new StringBuilder("");
        if ((originalViaHeaders != null) && (originalViaHeaders.length > 0))
        {
            for (Header viaHeader : originalViaHeaders)
            {
                via.append(viaHeader.getValue()).append(", ");
            }
        }
        via.append(response.getStatusLine().getProtocolVersion().toString()).append(" ").append(serverHostName);

        customerResponse.setHeader("via", via.toString());
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read response using HttpsUrlconnection when transfer encoding is chunked

From Dev

How does HTTP specify the end of a response for Transfer-encoding: chunked?

From Dev

Retrofit client and response with Transfer-Encoding: chunked

From Dev

Transfer-Encoding: chunked

From Dev

Transfer-Encoding: chunked

From Dev

CXF buffering data when using chunked encoding

From Dev

Empty request body when Transfer-Encoding is Chunked - NancyFX

From Dev

nginx chunked transfer encoding fails

From Dev

Node.js: how to disable chunked transfer-encoding?

From Dev

Read chunked response

From Dev

Disable "Transfer-Encoding:chunked" in Apache httpd

From Dev

Remove Transfer-Encoding:chunked in the POST request?

From Dev

Chunked transfer encoding - Page is not displayed properly

From Dev

Who set the Transfer-Encoding: chunked header?

From Dev

How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets?

From Dev

avoiding chunked encoding of HTTP/1.1 response

From Dev

Nginx Chunked Encoding Response Randomly Truncated

From Dev

how to disable chunked-transfer-encoding in grizzly based http-server

From Dev

Does Chunked Transfer Encoding work with Nancy, OWIN and HttpListener?

From Dev

Is it a good idea to use Transfer-Encoding: chunked on static files?

From Dev

Is there a wsgi server that will do progressive Transfer-Encoding: chunked

From Dev

GoogleTV Mediaplayer cannot handle http datasource with Transfer-Encoding : chunked

From Dev

CouchDB using chunked encoding with jQuery ajax()

From Dev

using trailer header with http chunked transfer. How to set cookie using it

From Dev

How to tell the HTTP server to not send chunked encoding

From Dev

How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?

From Dev

How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?

From Dev

XMLHttpRequest chunked response, only read last response in progress

From Dev

Content-Encoding: gzip + Transfer-Encoding: chunked with gzip/zlib gives incorrect header check

Related Related

  1. 1

    How to read response using HttpsUrlconnection when transfer encoding is chunked

  2. 2

    How does HTTP specify the end of a response for Transfer-encoding: chunked?

  3. 3

    Retrofit client and response with Transfer-Encoding: chunked

  4. 4

    Transfer-Encoding: chunked

  5. 5

    Transfer-Encoding: chunked

  6. 6

    CXF buffering data when using chunked encoding

  7. 7

    Empty request body when Transfer-Encoding is Chunked - NancyFX

  8. 8

    nginx chunked transfer encoding fails

  9. 9

    Node.js: how to disable chunked transfer-encoding?

  10. 10

    Read chunked response

  11. 11

    Disable "Transfer-Encoding:chunked" in Apache httpd

  12. 12

    Remove Transfer-Encoding:chunked in the POST request?

  13. 13

    Chunked transfer encoding - Page is not displayed properly

  14. 14

    Who set the Transfer-Encoding: chunked header?

  15. 15

    How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets?

  16. 16

    avoiding chunked encoding of HTTP/1.1 response

  17. 17

    Nginx Chunked Encoding Response Randomly Truncated

  18. 18

    how to disable chunked-transfer-encoding in grizzly based http-server

  19. 19

    Does Chunked Transfer Encoding work with Nancy, OWIN and HttpListener?

  20. 20

    Is it a good idea to use Transfer-Encoding: chunked on static files?

  21. 21

    Is there a wsgi server that will do progressive Transfer-Encoding: chunked

  22. 22

    GoogleTV Mediaplayer cannot handle http datasource with Transfer-Encoding : chunked

  23. 23

    CouchDB using chunked encoding with jQuery ajax()

  24. 24

    using trailer header with http chunked transfer. How to set cookie using it

  25. 25

    How to tell the HTTP server to not send chunked encoding

  26. 26

    How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?

  27. 27

    How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?

  28. 28

    XMLHttpRequest chunked response, only read last response in progress

  29. 29

    Content-Encoding: gzip + Transfer-Encoding: chunked with gzip/zlib gives incorrect header check

HotTag

Archive