How the server obtains the (city) address of the client user [get the real IP and get the city]

Source: Internet
Author: User

In web development, there are often needs, need to know the customer's current location (city), the general principle is this, first: through the Request object to obtain the remote user's IP address, second: third-party free (interface) services, through the IP to find out the user's city,

I am good at JSP, the following we have JSP as an example:

In the JSP, the method to obtain the IP address of the client is: Request.getremoteaddr (), which is valid in most cases. But in passing the Apache,nagix and other reverse proxy (here do not understand the reverse proxy click to open the link) software will not be able to obtain the client's real IP address. If the reverse proxy software is used, the IP address obtained with the REQUEST.GETREMOTEADDR () method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client.

After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request.

For example, when we visit the home page hangzhou.jsp, actually not our browser actually access to the server hangzhou.jsp file, but first by the proxy server Nagix to access hagnzhou.jsp, The proxy server then returns the result of the access to our browser, because it is the proxy server to access the hangzhou.jsp, so the IP obtained through the method of Request.getremoteaddr () in hangzhou.jsp is actually the address of the proxy server. is not the IP address of the client. But the head adds X-forwarded-for, and we can use Request.getheader ("X-forwarded-for") to determine if it's a proxy,

eg

Method one to obtain the client's real IP address:

Public String Getremortip (HttpServletRequest request) {
if (Request.getheader ("x-forwarded-for") = = null) {
return request.getremoteaddr ();
}
Return Request.getheader ("X-forwarded-for");
}


------------------------------------------------------------------------------------------------------------

Method of obtaining a client's real IP address two

Public String getipaddr (HttpServletRequest request) {
String IP = request.getheader ("X-forwarded-for");
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = Request.getheader ("Proxy-client-ip");
}
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = Request.getheader ("Wl-proxy-client-ip");
}
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = request.getremoteaddr ();
}
return IP;
}

Querying cities via IP

The IP query API over the network determines the city by IP address

This method IP library update is relatively fast. There are several common libraries, such as Google.

XML processing pages can be replaced by specialized processing tools such as Nokogiri.

There are many APIs to provide IP address query such as NetEase:


1.http://www.youdao.com/smartresult-xml/search.s?type=ip&q=ip Address

2. http://ip2loc.appspot.com/#含有详细实例讲解, but because the GWF address may not resolve

3. Sina's IP address query interface: HTTP://INT.DPOOL.SINA.COM.CN/IPLOOKUP/IPLOOKUP.PHP?FORMAT=JS

4. Sina multi-Region test method: Http://int.dpool.sina.com.cn/ipl ... amp;ip=218.192.3.42

#这个比较全, but there are individual IP parsing is not correct, hit a database is not updated it ....

5. Sohu IP Address query interface (default GBK): Http://pv.sohu.com/cityjson

6. Sohu IP Address Query interface (can be set code): Http://pv.sohu.com/cityjson?ie=utf-8

For more details please refer to: www.cnblogs.com/lwm-1988/archive/2012/07/02/2573003.html

I generally use Sina's: int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= behind the IP string to write

Example:

int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

Return a section of JS code:

var remote_ip_info = {"ret": 1, "Start": "218.192.0.0", "End": "218.192.7.255", "Country": "\u4e2d\u56fd", "province": "\ U5e7f\u4e1c "," City ":" \u5e7f\u5dde "," District ":" "," ISP ":" \u6559\u80b2\u7f51 "," type ":" \u5b66\u6821 "," desc ":" \ U5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662\u6559\u80b2\u7f51 "};

Use eval to execute this code: where country represents the country or region, province represents the province,......

Do not understand the place please leave a message, we progress together, the younger brother just unemployed, if the need for employment can call me, contact information see Personal data, Penguin number 641995773


How the server obtains the (city) address of the client user [get the real IP and get the city]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.