腾讯有一个接口可以获取你的外网IP,同时还解析了IP的地理位置,接口的地址是:http://fw.qq.com/ipaddress,这样我们就可以很方便的获取到这些信息了。
Java代码:
package com.aorsoft.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class IPTest {
public static void main(String[] args) {
try {
URL url = new URL("http://fw.qq.com/ipaddress");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(),"GBK"));
String line = null;
while ((line = reader.readLine()) != null)
System.out.println(line);
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
看看显示出了什么?
JS代码:
<script src="http://fw.qq.com/ipaddress" type="text/javascript"></script>
<script type="text/javascript">
function test(){
alert(IPData);
}
</script>