java 如何读取路由器的外网IP

lzj0470 2010-09-14 09:40:00
如题所示
...全文
555 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
雨天wxp 2011-12-30
  • 打赏
  • 举报
回复
public static routeIp getInstance() {
Authenticator.setDefault(new RouterPassAuth());
return ip;
}
这个方法,根本没用到,所以报错了,请给出正确调用方法!
lzj0470 2010-09-14
  • 打赏
  • 举报
回复
没问题,助人为快乐之本。
public class routeIp {

private static final routeIp ip = new routeIp();

public void routeIp(){

}

public static routeIp getInstance() {
Authenticator.setDefault(new RouterPassAuth());
return ip;
}

private static String getWanPacket() throws IOException {
URL url = new URL("http://192.168.1.1:80/userRpm/StatusRpm.htm?Connect=连 接&wan=1");
InputStream ins = null;
try {
ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String str;
StringBuffer wanPacket = new StringBuffer();
while ((str = reader.readLine()) != null) {
wanPacket.append(str);
}
return wanPacket.toString();
}finally{
if(ins!=null){
ins.close();
}
}
}

public static void main(String[] args) {
routeIp ip = routeIp.getInstance();
try {
System.out.println(ip.getWanPacket());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public class RouterPassAuth extends Authenticator {

@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(adslIp.ROUTE_USER,
adslIp.ROUTE_PWD.toCharArray());
}

}
clariones 2010-09-14
  • 打赏
  • 举报
回复
你是在家里写程序,需要ADSL路由器的公网地址吧?
具体看设备的接口了,如果这样倒是可以用java来搞,起码模拟访问管理界面就可以,不过比较麻烦。
你是怎么搞定的,给大家分享下吗!
lzj0470 2010-09-14
  • 打赏
  • 举报
回复
终于搞定了。谢谢各位了。我不是需要用类似www.xx.com,然后在获取我的外网IP。我是用JAVA程序直接读取路由器的WAN IP。散分。
bayougeng 2010-09-14
  • 打赏
  • 举报
回复
不借助外网程序的帮助,一个单独的PG在内网应该不大可能办到吧。
Mybeautiful 2010-09-14
  • 打赏
  • 举报
回复
程序访问 网站 http://www.ip.cn/ (可以使用 httpclient)
然后解析html, 就能找到你的外部ip
lzj0470 2010-09-14
  • 打赏
  • 举报
回复
我明白你的意思。但是我不是要这种。
菖蒲老先生 2010-09-14
  • 打赏
  • 举报
回复
深奥。
clariones 2010-09-14
  • 打赏
  • 举报
回复
这个和Java无关。 外网IP是路由器的配置,它不公开访问接口你是看不到的。
可以采取折中的办法:在内网发个包到外网的某个程序,这个程序鉴别出是你的包,就把外网IP读出来发给你。
我在研究流媒体协议时看到他们有个办法就是这样过防火墙的。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧