TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
腾讯有一个接口可以获取你的外网IP,同时还解析了IP的地理位置,接口的地址是:http://fw.qq.com/ipaddress,这样我们就可以很方便的获取到这些信息了。
- 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();
- }
- }
- }
复制代码 看看显示出了什么?
源码下载:http://file.javaxxz.com/2014/11/4/000216046.zip |
|