【发布时间】:2014-03-26 21:45:13
【问题描述】:
我正在 JavaSE 应用程序中启动一个 WebService,我想获取客户端的调用方 IP。 这可能吗?
我写了一个小测试用例:
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import javax.jws.*;
import javax.xml.ws.Endpoint;
import com.sun.net.httpserver.*;
@WebService
public class TestWs {
@WebMethod public String testMethod(String param) {
String clientHostIp = ""; // how to obtain client's host ?
return "hello "+ clientHostIp;
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
HttpServer thttpserver = HttpServer.create(new InetSocketAddress("0.0.0.0", 2000),8);
final HttpServer httpserver = thttpserver;
httpserver.setExecutor(executorService);
httpserver.start();
HttpContext ctx = httpserver.createContext("/TestWs");
final Endpoint wsendpoint = Endpoint.create(new TestWs());
wsendpoint.publish(ctx);
}
}
【问题讨论】:
-
查看stackoverflow.com/questions/12727989/jax-ws-getting-client-ip 请注意这个限制:如果您的应用服务器位于 Web 或代理服务器之后,则 IP 很可能是该主机的 IP,而不是实际的客户端。
标签: java web-services jax-ws endpoint