【发布时间】:2019-08-11 19:26:56
【问题描述】:
我需要应用程序显示我当前的 IP 地址。
在这种情况下,我们有这样的东西(获取请求)
我需要从这里获取ip
public class Main {
public static void main(String[] args) throws Exception {
String url = "http://2ip.ru/";
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
【问题讨论】: