Skip to Content
author's profile photo Former Member
Former Member

Capturing of IP Address through Java Code

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?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 20, 2014 at 12:01 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 20, 2014 at 12:39 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 20, 2014 at 03:26 PM

    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?

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.