Program for Public IP Capture

Asked

Viewed 773 times

2

I need to implement in my program a method to capture the user’s Public IP... remembering that this IP must be stored, even before the user establishes any kind of connection.

When running the program, the client must call a method for capturing this IP, and it will be stored in a variable within this client...

How should I proceed ?

  • 1

    What is the difference of this question to your previous question?

  • 1

    the difference is that the previous question captures ip with a socket connection. this question, needs a method for capturing ip before any connection

  • And here, it would be http?

  • I thought to put in the program something like accessing a site whatismyip for example and store the result in some variable, but I’m having problems for this : (

  • 1

    Do you want this IP to be obtained from the client itself, or from the server when contacted by a client? Your last comment suggests that it is something that does not involve the server in any way. Could you give more details than you intend to do? (feel free to [Dit] the question as necessary)

  • 1

    @mgibsonbr changed the question, I hope it is more enlightening. :)

Show 1 more comment

2 answers

2

The most direct way to get your public IP is - as you yourself suggested - to access a website that will return that information to you. If there are no drawbacks, why not your own website/application?

But if you need an external service, there are several you choose - not all so simple to use and/or free (whatismyip.com for example has an API, but only for a paid plan, and with limit of daily requests). Here are some options, the simplest and "no frills" is the hazicanip.com (or canihazip.com/s) - it returns you the IP and ready, without html, body, or nothing! So to access it just do:

URL url = new URL("http://icanhazip.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String ip = in.readLine();
in.close();

Maybe it’s interesting - in case your main source is offline - try some alternative sources. Others that return the "raw" IP are the ifconfig.me/ip and the ipinfo.io/ip (same procedure above).

Source: that question on Soen

  • 1

    Thanks comrade, I was really with this idea kkkk, but I was having problems at the time of doing.

0

If it is HTTP, just do the following

package servlet;  

import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.*;  
import javax.servlet.http.*;  

public class ServletIP extends HttpServlet   
{  
   private static final String CONTENT_TYPE = "text/html; charset=ISO-8859-1";  

   public void init(ServletConfig config) throws ServletException  
   {  
      super.init(config);  
   }  

   public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
   {  
      response.setContentType(CONTENT_TYPE);  
      PrintWriter out = response.getWriter();  
      out.println(request.getRemoteAddr());  
      out.close();  
   }  
}  

And then the mapping:

<servlet>  
  <servlet-name>ServletIP</servlet-name>  
  <servlet-class>servlet.ServletIP</servlet-class>  
</servlet>  
<servlet-mapping>  
  <servlet-name>ServletIp</servlet-name>  
  <url-pattern>/ip</url-pattern>  
</servlet-mapping>

Loading this into a Java EE server, sending any request to www.ADDRESSEB/contexto_do_conteiner/ip , it returns the person’s IP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.