简体   繁体   中英

Http request.getIntHeader() returns a NumberFormat Exception

I made a Http request and this is how the request header looks like:

GET /unni/servlets/servlet/HelloWorldExample HTTP/1.1
Host: localhost:8700
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0

Then, I just used request.getIntHeader("User-Agent") .This time I got a NumberFormatException saying that 'request header cannot be converted to an integer'.This is what API and servlet spec also say.I searched for more about this to get some working example, but did not get any desired results.So,how can I use this method? I am using Apache Tomcat 7.0.25 .

Thanks in advance.

Header with key "User_Agent" contain mixed date type value(combination of String and Integer), in your case it is :User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0.

If you trying to get this value as Integer you definitely get NumberFormatException (because header value contain non-numeric value as well).

you can only apply request.getIntHeader() to those header which contain purely numeric value. you can define your own header key with numeric value and retrieve this using request.getIntHeader().

I don't understand, the User-Agent header is a string, you could retrieve it like
String userAgent = request.getHeader("User-Agent");
Or am I missing the point of your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM