发起HTTP请求获取IP的网站然后用正则过滤出IP地址
发起HTTP请求并不是100%成功 来个死循环直到拿到IP为止
public int getServerPort() {
return serverPort;
}
private int serverPort;
public String getUrl() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
String v4IP = "";
while (StrUtil.isBlank(v4IP)){
v4IP = getV4IP();
}
return "http://"+v4IP+":"+this.serverPort;
}
public String getHost() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
return address.getHostAddress();
}
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
serverPort = event.getWebServer().getPort();
}
public static String getV4IP(){
String ip = "";
String url = "https://api.live.bilibili.com/client/v1/Ip/getInfoNew";
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = client.newCall(request).execute();
if(ObjectUtil.isNotNull(response)){
String data = response.body().string();
Object succesResponse = JSON.parse(data);
ip = JsonUtils.getValueByKey(succesResponse, "addr").toString();
}
} catch (Exception e) {
System.err.println(e);
ip="";
}
return ip;
}