1

I'm trying to develop a webapp that can extract the IP address, the webbrowser's description and the country of the enduser's request.

But I can't find how to deal that. How can I do this, please ?

Beryllium
  • 12,808
  • 10
  • 56
  • 86
meee meeee
  • 191
  • 1
  • 3
  • 15

2 Answers2

1

This information is provided by javax.servlet.HttpServletRequest:

  • IP address: request.getRemoteAddr()
  • Web browser description (the "user agent"): request.getHeader("user-agent")

There is normally no country information, so you have to use a geolocation service together with the IP address.

For example http://freegeoip.net: Use an URL like

http://freegeoip.net/xml/www.stackoverflow.com

Other formats (CSV, JSON) are possible, it's described on their home page.

Possibly helpful:

  • request.getHeader("accept-language"): Contains a list of languages (like "en", "de", "fr", ...)

Note: There is always the IP address, but request headers are optional.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • That's interesting, but, how can I include this in my java code knowing that I need to get the enduser's country not the of a web site ? There is any documentations about that please ? – meee meeee Sep 06 '13 at 18:23
  • That's interesting, but, how can I include this in my java code knowing that I need to get the enduser's country not of a web site ? There is any documentations about that please ? – meee meeee Sep 06 '13 at 18:36
  • Just use the value from `request.getRemoteAddr()` as the argument in the URL for freegeoip. – Beryllium Sep 06 '13 at 18:49
  • I've tested your suggestion in localhost, but, I've got this `0:0:0:0:0:0:0:1` as IP, `fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4` as language, `Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36` as a webbrowser's description knowing that I'm running it with Chrome. So I've got an error when I've tried to read the XML file as i've a wrong ip adress. – meee meeee Sep 06 '13 at 20:15
  • Of course you cannot "locate" a localhost IP address. Just try it with a real public IP address. – Beryllium Sep 09 '13 at 11:36
0

There is quite a bit of info on this available. Much depends on what you are running on the server: How to get client's IP address using javascript only?, Getting a user country name from originating IP address with Ruby on Rails

Community
  • 1
  • 1
cage rattler
  • 1,587
  • 10
  • 16