IBM Worklight 6 - 如何在适配器端获得客户端IP地址 [英] IBM Worklight 6 - How would i get client IP address on adapter side

查看:220
本文介绍了IBM Worklight 6 - 如何在适配器端获得客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在适配器端有客户端IP地址,但我不知道什么是worklight api。我搜索它但没有运气。

I want to have client ip address on adapter side but i don't know what is the worklight api for that. I search for it but no luck.

我在客户端代码上使用了这个API,如下所示

I used this api on client side code which is given below

WL.Device.getNetworkInfo(function (networkInfo) {
        console.log ("Ip address of device "+networkInfo.ipAddress);
       });

它工作正常,我可以从客户端传递给适配器。但我只是想知道在适配器程序中是否可以在服务器端实现相同的东西。

It works fine and i can pass this to the adapter from client side. But i just wanted to know whether the same thing can be implemented on server side in adapter procedure.

我还使用了下面给出的代码

And I also used this code which is given below

var request = WL.Server.getClientRequest();
    var userAgent = request.getHeader("User-Agent");

我们可以在适配器程序中使用此API获取IP地址吗?

Can we get Ip address here using this API in adapter procedure.

推荐答案

WL.Server.getClientRequest()将返回对HttpServletRequest Java对象的引用( http://docs.oracle.com/javaee/6/api/javax/servlet/http/ HttpServletRequest.html )。您可以使用Java API获取所需的信息,例如

WL.Server.getClientRequest() will return a reference to HttpServletRequest Java object (http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html). You can use Java APIs to get the info you need, e.g.

var request = WL.Server.getClientRequest();
request.getRemoteAddr()
request.getRemoteHost()

请注意如果客户端和您的WL服务器之间存在网关/代理(并且很可能是)以上API将为您提供有关代理的信息。如果您需要实际的设备IP,您可以使用

Note that in case there are gateways/proxies between client and your WL server (and there most probably are) above APIs will get you info about proxies. In case you need the actual device IP you can use

var request = WL.Server.getClientRequest();
var IPAddress = request.getHeader('x-forwarded-for'); 






更新:


UPDATE:

为了迭代头枚举并获取请求头的完整列表,请使用以下代码:

In order to iterate over headers enumeration and get the full list of request headers use following code:

    var headers = {};

var request = WL.Server.getClientRequest();
var headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()){
    var headerName = headerNames.nextElement();
    var headerValue = request.getHeader(headerName);
    headers[headerName] = headerValue;
}

这篇关于IBM Worklight 6 - 如何在适配器端获得客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆