apache-httpclient-5.x

Is there any way to get Headers from HttpResponse in HttpClient5?


In Apache HTTP Components 4, I could get Header[] from HttpResponse by using the method getAllHeaders(), cause this method extends from class HttpMessage. However, after upgrading my dependency to 5.x, I found that this method has been removed from class HttpResponse. The only solution I found is to change HttpResponse to CloseableHttpResponse then use method getHeaders(). Is this the correct approach?

Is there any way to get Headers from HttpResponse in HttpClient5?


Solution

  • Both HttpRequest and HttpResponse extend a common base interface MessageHeaders. There is #getHeaders method that returns an array of all headers contained in the message.

    However I would strongly recommend using #headerIterator as it is likely to be more memory efficient, especially for larger messages.