Urbanleg
Urbanleg

Reputation: 6532

HTTP-Accept header is different on several methods

I'm trying to get a request's http-accept header and I get different values when I get the header from

  1. Java - req.getHeader("Accept") + req.getHeader("Accept-Encoding") + req.getHeader("accept-language")
  2. PHP - '<?php echo $_SERVER['HTTP_ACCEPT'].$_SERVER['HTTP_ACCEPT_ENCODING'].$_SERVER['HTTP_ACCEPT_LANGUAGE']; ?>

The results are :

  1. */*gzip,deflate,sdchen-US,en;q=0.8
  2. text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8gzip,deflate,sdchen-US,en;q=0.8

How come they are different?

Am I getting them wrong?

Upvotes: 1

Views: 287

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279910

PHP is returning all of the request parameters while your Java HTTP client is only returning one.

The HttpServletRequest method provides the getHeaders(String) that

Returns all the values of the specified request header as an Enumeration of String objects.

So use that.

Notice how the Java result (split to account for the String concatenation you are doing)

*/*                                                                          */
^ the last 'Accept' header value

gzip,deflate,sdch 
^ the 'Accept-Encoding'

en-US,en;q=0.8
^ the 'Accept-Language'

I'm not exactly sure why it doesn't do it for the other headers.


This would be much clearer from the start if your output was easier to read. Don't just concatenate Strings like that. Use a separator of some sort.

Upvotes: 1

Related Questions