Getting request/response headers

Getting the request headers can be done using the HttpRequest.headers() method. A similar method exists in HttpResponse for getting the headers of the response. Both methods return an HttpHeaders object.

Both of these methods can be used in the same way, so let's focus on getting the response headers. We can get the headers like so:

HttpResponse<...> response ...
HttpHeaders allHeaders = response.headers();

Getting all of the values of a header can be done using HttpHeaders.allValues(), as follows:

List<String> allValuesOfCacheControl
= response.headers().allValues("Cache-Control");

Getting only the first value of a header can be done using HttpHeaders.firstValue(), as follows:

Optional<String> firstValueOfCacheControl
= response.headers().firstValue("Cache-Control");
If the returned value of a header is Long, then rely on HttpHeaders.firstValueAsLong(). This method gets an argument representing the name of the header and returns Optional<Long>. If the value of the specified header cannot be parsed as Long, then NumberFormatException will be thrown.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset