/**

* 获取公网ip

* @return

*/

public String getNetIp() {

String ip = "";

InputStream inStream = null;

try {

URL infoUrl = new URL("http://1212.ip138.com/ic.asp");

URLConnection connection = infoUrl.openConnection();

HttpURLConnection httpConnection = (HttpURLConnection) connection;

int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

inStream = httpConnection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gb2312"));

StringBuilder builder = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {

builder.append(line);

//builder.append(line).append("\n");

}

inStream.close();

int start = builder.indexOf("[");

int end = builder.indexOf("]");

ip = builder.substring(start + 1, end);

return ip;

}

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明获取公网ip!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐