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()); }
可以采取折中的办法:在内网发个包到外网的某个程序,这个程序鉴别出是你的包,就把外网IP读出来发给你。
我在研究流媒体协议时看到他们有个办法就是这样过防火墙的。
然后解析html, 就能找到你的外部ip
具体看设备的接口了,如果这样倒是可以用java来搞,起码模拟访问管理界面就可以,不过比较麻烦。
你是怎么搞定的,给大家分享下吗!
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());
}
}
Authenticator.setDefault(new RouterPassAuth());
return ip;
}
这个方法,根本没用到,所以报错了,请给出正确调用方法!