Javaweb--GET request IP (with implementation source)

Source: Internet
Author: User

    • The implementation steps are as follows :

(1) Create a new Java Web project

(2) New servers package, new class Getrequestip, used to process GET request Ip,getrequestip class complete code as follows:

 Packageservers;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classGetrequestip { Public Static voidPost (HttpServletRequest request, httpservletresponse response) {getipaddress (request); }         Public Staticstring getipaddress (HttpServletRequest request) {string IP=NULL; IP= Request.getheader ("X-forwarded-for"); System.out.println ("X-forwarded-for:" +IP); if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("X-forwarded-for"); System.out.println ("Request.getheader (\" x-forwarded-for\ ") =" +IP); }        if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Proxy-client-ip"); System.out.println ("Request.getheader (\" proxy-client-ip\ ") =" +IP); }        if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Wl-proxy-client-ip"); System.out.println ("Request.getheader (\" wl-proxy-client-ip\ ") =" +IP); }        if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Http_client_ip"); System.out.println ("Request.getheader (\" http_client_ip\ ") =" +IP); }        if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Http_x_forwarded_for"); System.out.println ("Request.getheader (\" http_x_forwarded_for\ ") =" +IP); }        if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP=request.getremoteaddr (); System.out.println ("request.getremoteaddr () =" +IP); }        if(NULL! = IP && ip.indexof (', ')! =-1)        {            //if the multi-level reverse proxy is passed, the value of x-forwarded-for is more than one, but a string of IP values//take the first non-unknown valid IP string in x-forwarded-for//such as: x-forwarded-for:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100//User Real IP: 192.168.1.110//Note: When the access address is localhost, the address format is 0:0:0:0:0:0:1System.out.println ("ip=" +IP); String[] IPs= Ip.split (",");  for(inti = 0; i < ips.length; i++)             {                if(NULL! = Ips[i] &&! " Unknown. Equalsignorecase (Ips[i])) {IP=Ips[i];  Break; }            }            if("0:0:0:0:0:0:1". Equals (IP)) {System.out.println ("Because the client access address uses localhost, get the client real IP address error, please use IP access"); }        }        if("Unknown". Equalsignorecase (IP)) {System.out.println ("Due to client access through Squid reverse proxy software, to obtain the real IP address of the client error, please change the squid.conf profile Forwarded_for item is on by default is resolved"); }                        returnIP; }}

(3) Create a new JSP page named myrequest.jsp, complete with the following code:

<%@ Page Import="servers. Getrequestip" %><%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><%out.clear (); out=Pagecontext.pushbody (); Getrequestip.post (request, response);%>

(4) Run, turn on Tomcat, enter the URL in the browser: http://127.0.0.1:8085/MyDemo/MyRequest.jsp, view the output.

    • Attached: When obtaining the client IP address, the difference between three HTTP request headers when using a different proxy

(Learn from:http://www.cnblogs.com/technic-emotion/articles/3701257.html)

One, not using the proxy server situation:

    • REMOTE_ADDR = Your IP
    • Http_via = no value or no display
    • Http_x_forwarded_for = no value or no display

  Second, the use of transparent proxy server situation: Transparent Proxies

    • REMOTE_ADDR = Last Proxy server IP
    • Http_via = Proxy Server IP
    • Http_x_forwarded_for = Your real IP, after multiple proxy servers, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

This type of proxy server also forwards your information to your Access object, and does not achieve the purpose of hiding the true identity.

  Third, the use of ordinary anonymous proxy server situation: Anonymous Proxies

    • REMOTE_ADDR = Last Proxy server IP
    • Http_via = Proxy Server IP
    • Http_x_forwarded_for = Proxy IP, after multiple proxies, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

In this case, your real IP is hidden, but the access to the object reveals that you are using a proxy server to access them.

  Iv. use of deceptive proxy servers: distorting Proxies

    • REMOTE_ADDR = Proxy Server IP
    • Http_via = Proxy Server IP
    • Http_x_forwarded_for = Random IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Tells the Access object that you used a proxy server, but fabricated a bogus random IP to spoof it instead of your real IP.

  V. Use of highly anonymous proxy servers: High anonymity Proxies (Elite Proxies)

    • REMOTE_ADDR = Proxy Server IP
    • Http_via = no value or no display
    • Http_x_forwarded_for = no value or not displayed, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Full use of Proxy server information replaces all of your information, as if you were using the proxy server directly to access the object.

Javaweb--GET request IP (with implementation source)

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.