/**
* 获取本机外网ip
*
*/
public static String getPublicIp() {
try {
String ip = "http://pv.sohu.com/cityjson?ie=utf-8";
URL url = new URL(ip);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String read;
while ((read = in.readLine()) != null) {
stringBuilder.append(read);
}
//可打印stringBuilder再解析得到的信息
String string = StringUtils.split(stringBuilder.toString(), "=")[1];
JSONObject jsonObject = JSONUtil.parseObj(string);
return jsonObject.get("cip").toString();
} catch (Exception e) {
return "未知";
}
}
腾讯定位:根据ip解析地址信息
//发送get请求并接收响应数据
Map<String, Object> map = new HashMap<>();//存放参数
map.put("ip", clientIP);
map.put("key", key);
String result= HttpUtil.createGet(baseUrl).form(map).execute().body();
JSONObject jsonObject = JSONObject.parseObject(result);
dingwei:
baseUrl: http://apis.map.qq.com/ws/location/v1/ip
key: NOHBZ-XMUY4-QSFUQ-DAHQX-P5ICV-JPBHZ