0

我正在通过 stub 连接到 Axis 2 服务器:

MyServiceStub fcc = new MyServiceStub(url);

建立连接后,由于我在不同子网上的机器上配置了不同的 IP 地址,我想知道与服务器建立连接的地址是什么。

例如,如果我有一台以这种方式配置了 2 个 NIC 和 IP 地址的机器:

eth0 ---> 192.165.0.1
eth1 ---> 10.0.3.1

我在 IP 上有一个 Axis 服务器:10.0.3.33,一旦建立连接,我怎么知道有界的 IP 地址?

如何获取连接的客户端 IP 地址(在这种情况下:)10.0.3.1

4

2 回答 2

0

也许你可以尝试这样的事情

request.getHeader("url");
String ipAddress = request.getHeader("X-FORWARDED-FOR");
   if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
   }
于 2013-07-30T09:53:48.293 回答
0

If you are using https protocol to invoke axis2 web service, you can implements your own SecureProtocolSocketFactory like this:

public class CustomProtocolSocketFactory implements SecureProtocolSocketFactory {
    private static final Logger logger = Logger.getLogger("trace");
    InetAddress localAddress=null;
    SSLContext ctx=null;
    Socket socket=null;
    public CustomProtocolSocketFactory(SSLContext ctx) {
        this.ctx = ctx;
    }
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress) {
        this.ctx = ctx;
        this.localAddress=localAddress;
    }
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress, Socket socket) {
        this.ctx = ctx;
        this.localAddress=localAddress;
        this.socket=socket;
    }
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
            throws IOException, UnknownHostException {
        logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort)");
        return ctx.getSocketFactory().createSocket(host, port, localAddress, localPort);
    }
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
            HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
        logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort,HttpConnectionParams params)");
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be null");
        }
        int timeout = params.getConnectionTimeout();
        SocketFactory socketfactory = ctx.getSocketFactory();
        if (timeout == 0) {
            if(this.localAddress!=null){
                return socketfactory.createSocket(host, port, this.localAddress, localPort);
            }
            else{
                return socketfactory.createSocket(host, port, localAddress, localPort);
            }
        } else {
            Socket socket = socketfactory.createSocket();
            if(this.localAddress!=null){
                SocketAddress localaddr = new InetSocketAddress(this.localAddress, localPort);
                SocketAddress remoteaddr = new InetSocketAddress(host, port);
                socket.bind(localaddr);
                socket.connect(remoteaddr, timeout);
                return socket;
            }
            else{
                SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
                SocketAddress remoteaddr = new InetSocketAddress(host, port);
                socket.bind(localaddr);
                socket.connect(remoteaddr, timeout);
                return socket;
            }
        }
    }
    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        if(localAddress!=null){
            logger.debug("createSocket(String host, int port) localAddress not null");
            return createSocket(host, port, localAddress, 0);
        }
        else{
            logger.debug("createSocket(String host, int port)");
            return ctx.getSocketFactory().createSocket(host, port);
        }
    }
    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
            throws IOException, UnknownHostException {
        logger.debug("createSocket(Socket socket, String host, int port, boolean autoClose)");
        return ctx.getSocketFactory().createSocket(socket, host, port, autoClose);
    }
}

in a way that you can bind the socket with InetAddress localAddress and use like this:

proxy._getServiceClient().getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                            new Protocol("https",(ProtocolSocketFactory)new CustomProtocolSocketFactory(context,localAddress),port));
于 2017-10-23T07:57:56.050 回答