문제

How to determine the IP address that originate the request? My current code:

@Override
public Response serve(String uri, Method method,
    Map<String, String> headers, Map<String, String> parms,
    Map<String, String> files) {

    headers.get("origin"); // my current workaround
};

But any client can modify or remove request headers. So, this is not a proper way to get that information. In ASP.NET MVC3, this is can be achieved via Request.UserHostAddress.

도움이 되었습니까?

해결책

Right now, NanoHTTPD doesnt expose the raw sockets. There is a request to add "getSocket()" or similar to the session class and that will allow interrogation of the IP address and solve what you're looking for. Give me a couple of days to get the code together and check back & we should have you on your way!

[EDIT]

In your "serve()" method, if you call

Map<String, String> headers = session.getHeaders();

And then, look for either "remote-addr" or "http-client-ip" in the map, you ought to have what you need. That is, the result of calling "getInetAddress()" on the server socket (which according to the Java documentation is the "the remote IP address to which this socket is connected"). Note, if those headers arent present in the map it'll probably be down to you having an older version of the code. Version 2.0.5 was released today, and they're definitely present.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top