cancel
Showing results for 
Search instead for 
Did you mean: 

Capturing of IP Address through Java Code

Former Member
0 Kudos

Dear All,

I have a situation in my Webdynpro Java Application where I have to capture the IP address of the end user's PC. I use the below code for capturing IP:

IWDRequest req = WDProtocolAdapter.getProtocolAdapter().getRequestObject();

String ipAddress = req.getClientHostName();

The problem is, the code is working perfectly in Quality system, but not in Production system. The IP which is captured in the Production system is the Portal IP itself. Is the problem has to do with some Network settings or the code has to be changed?

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Patralekha,

Thanks for that helpful link you provided. I was able to do some workarounds by adding servlet.jar as external library to my DC. The below code is what I wrote in my DC:

HttpServletRequest request=(HttpServletRequest)TaskBinder.getCurrentTask().getProtocolAdapter().getRequestObjectInternal().getProtocolRequest();

String ip = request.getHeader("X-Forwarded-For");

messageManager.reportSuccess("X-Forwarded-For: "+ip);

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

       ip = request.getHeader("Proxy-Client-IP");

       messageManager.reportSuccess("Proxy-Client-IP: "+ip);

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

       ip = request.getHeader("WL-Proxy-Client-IP");

       messageManager.reportSuccess("WL-Proxy-Client-IP: "+ip);

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

       ip = request.getHeader("HTTP_CLIENT_IP");

       messageManager.reportSuccess("HTTP_CLIENT_IP: "+ip);

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

       ip = request.getHeader("HTTP_X_FORWARDED_FOR");

       messageManager.reportSuccess("HTTP_X_FORWARDED_FOR: "+ip);

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

      ip = request.getRemoteAddr();

      messageManager.reportSuccess("getRemoteAddr: "+ip);

      ip = null

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))

{

     ip = request.getRemoteHost();

       messageManager.reportSuccess("getRemoteHost: "+ip);

}

For all the messages except the last two messages, I get null as the answer. For request.getRemoteAddr() and request.getRemoteHost(), it is returning the proxy.

Can anyone please help on this or guide me in the right path?

Former Member
0 Kudos

There is one issue in

request.getRemoteAddr()


The problem, is that the IP we get is not always the correct IP. For example if our server is behind a load balancer, the method “request.getRemoteAddr” returns the IP of the load balancer and not the IP of the remote client. Another common example, is that the client is behind some proxy or even several proxies. The IP we will get will not be the correct IP.


Check following link:

Coder Eye: Get real IP from request in Java

Former Member
0 Kudos

Thanks for the link Patralekha.

I see that, there also the code accesses 'X-Forwarded-For' parameter. It seems that parameter will be false by default in SAP Webdispatcher. We have asked the basis team to configure that. Since it is PRD server, unfortunately we can't do that at this time. Once that is done and if my code works, I will update this place

Former Member
0 Kudos

Can you please try following code.


You may not get the real client IP if a the client is behind a proxy, you will get the IP of the proxy and not the client. However, the proxy may include the requesting client IP in a special HTTP header.

HttpServletRequest req=(HttpServletRequest)TaskBinder.getcurrentTask().getProtocolAdapter().getRequestObjectInternal().getProtocolRequest();

req.getHeader("X-Forwarded_For);

Thanks,

Patralekha

Former Member
0 Kudos

Hi Patralekha,

Thanks for replying. For using the HttpServletRequest in my WD Java code, I need to add the servlet.jar into my DC right? I'll try that way and comeback.

Former Member
0 Kudos

Hi Sujai

There was a similar issue faced by me while in ITS.

You have to cross check with internet settings+port+clear histrory and other popins.

Also in the code where URL is provided : Check the path should be proper, not refering to sap logon files present in local drives, so as to make system independent.

Former Member
0 Kudos

Hi Sunita,

Thanks for taking time to reply. Can you be more clearer regarding the Internet Settings, port and other popins that needs to be checked?