1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java获取本机外网ip

java获取本机外网ip

时间:2020-03-12 05:23:58

相关推荐

java获取本机外网ip

参考了一些博客,发现大部分都是通过访问外网再进行获取外网ip,本文是通过访问chinaz的接口返回的ip信息直接获取,有更好的办法可以指教一下,感谢!

package com.platform.api;import java.io.BufferedReader;import java.io.InputStreamReader;import .URL;import .www.protocol.http.HttpURLConnection;/*** @author ajiu*/public class IpUtils {public static String getIP() {// String ip = "/getip.aspx";String ip = "/cityjson?ie=utf-8";String inputLine = "";String read = "";try {URL url = new URL(ip);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));while ((read = in.readLine()) != null) {inputLine += read;}} catch (Exception e) {e.printStackTrace();}return inputLine;}public static void main(String[] args) {System.out.println(getIP());}}

/4/23

搜狐接口/cityjson?ie=utf-8(默认GBK,可指定编码)

发现之前的接口已被限制,特在此修改

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。