如何从桌面应用程序调用我们的网络服务获取 public ip

How to get public ip from desktop application call to our webservice

我需要捕获调用我们 api 的系统的 public ip,该系统是调用我们其余 api 的桌面应用程序,我正在使用以下代码

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public @ResponseBody JSON_Response sendMessage(@RequestBody Objectjson objjson,HttpServletRequest
            request) {

        LOG.debug("sending message via payless server");
        inputjson.setSentFrom("Message Sent From Api");
    //  inputjson.setHostip(""+request.getRemoteHost());
//I am using following to capture it
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getLocalAddr());
        LOG.debug("--------------------------"+request.getRemoteHost());
        LOG.debug("--------------------------"+request.getPathInfo());
        LOG.debug("--------------------------"+request.getPathTranslated());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        LOG.debug("--------------------------"+request.getRemoteUser());
        JSON_Response response = sendMessageInterface.processInput(inputjson);
        LOG.debug("sending response message " + response.getStatusDescription());

        return response;
    }

我在 ip address.If 中获取我自己的服务器 ip address.If 我从邮递员那里调用其余 api 我正在获取正确的 ip 地址。

如果您找到任何其他检索 public ip 的方法,请告诉我。

我正在使用 Wildfly 服务器 wildfly-10.1。0.Final

这是我用来获取远程用户IP地址的方法。希望对你有帮助

public HttpServletRequest getRequest() {
  RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
  if (attribs instanceof ServletRequestAttributes) {
    HttpServletRequest request = ((ServletRequestAttributes)attribs).getRequest();
    return request;
  }
  return null;
}

public String getClientIp() {
  String remoteAddr = "";
  HttpServletRequest request = getRequest();
  if (request != null) {
    remoteAddr = request.getHeader("X-FORWARDED-FOR");
    if (remoteAddr == null || remoteAddr.trim().isEmpty()) {
      remoteAddr = request.getRemoteAddr();
    }
  }
  return remoteAddr;
}