Advertisement
dev017

pegar ip remotamente usando api do IPify (java)

Jul 30th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. public class ObterIPRemotamente {
  8.     public static void main(String[] args) {
  9.         try {
  10.             URL url = new URL("https://api.ipify.org");
  11.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  12.             connection.setRequestMethod("GET");
  13.  
  14.             BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  15.             String response = reader.readLine();
  16.  
  17.             System.out.println("EndereΓ§o IP: " + response);
  18.  
  19.             reader.close();
  20.             connection.disconnect();
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement