以下四种方式都可以查询到公网的出口ip
- https://ifconfig.me/ip
- https://myip.ipip.net/s
- https://ip.useragentinfo.com/json
- http://ip-api.com/json
以下是获取ip的java代码:
static private String getIpFromMyIp() {
String ipRet = OkHttpRequestUtil.requestGet("https://myip.ipip.net/s");
if (StringUtils.isBlank(ipRet)) {
return "";
}
return StringUtils.trim(ipRet);
}
static private String getIpFromUserAgent() {
String ipRet = OkHttpRequestUtil.requestGet("https://ip.useragentinfo.com/json");
if (StringUtils.isBlank(ipRet)) {
return "";
}
try {
JSONObject jsonObject = JSON.parseObject(ipRet);
return StringUtils.trimToEmpty(jsonObject.getString("ip"));
} catch (Exception e) {
logger.error("getIpFromUserAgent error", e);
}
return "";
}
// http://ip-api.com/json
static private String getFromIpApi() {
String ipRet = OkHttpRequestUtil.requestGet("http://ip-api.com/json");
if (StringUtils.isBlank(ipRet)) {
return "";
}
try {
JSONObject jsonObject = JSON.parseObject(ipRet);
return StringUtils.trimToEmpty(jsonObject.getString("query"));
} catch (Exception e) {
logger.error("getFromIpApi error", e);
}
return "";
}
附ok http的代码:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
public class OkHttpRequestUtil {
private static final Logger logger = LoggerFactory.getLogger(OkHttpRequestUtil.class);
public static OkHttpClient okHttpClient;
static {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(500);
dispatcher.setMaxRequestsPerHost(500);
okHttpClient = (new OkHttpClient.Builder())
.connectTimeout(Duration.ofSeconds(5))
.callTimeout(Duration.ofSeconds(5))
.readTimeout(Duration.ofSeconds(5))
.writeTimeout(Duration.ofSeconds(5))
.connectionPool(new ConnectionPool(100, 120L, TimeUnit.MINUTES))
.dispatcher(dispatcher).build();
}
public static String requestGet(String url) {
return request("get", url, null);
}
private static String request(String method, String urlPath, String jsonString) {
Request.Builder builder = (new Request.Builder()).url(urlPath);
if ("post".equals(method)) {
RequestBody requestBody;
if (jsonString != null) {
requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonString);
} else {
requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), new byte[0]);
}
builder.post(requestBody);
}
Request request = builder
.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36")
.addHeader("Content-Type", "application/json;charset=UTF-8")
.addHeader("Accept", "application/json, text/plain, */*")
.addHeader("Accept-Encoding", "gzip, deflate, br")
.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response != null && response.body() != null) {
return response.body().string();
}
} catch (Exception e) {
logger.error("request error", e);
}
return "";
}