当前位置:   article > 正文

获取公网ip的几种方式_怎么获取公网ip

怎么获取公网ip

以下四种方式都可以查询到公网的出口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 "";
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

ok http的代码:

			<dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.14.9</version>
            </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

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 "";
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/229911
推荐阅读
相关标签
  

闽ICP备14008679号