1、路由器的用户名和密码我是知道,这里不是hacker技术,是为了方便
2、借助了Fiddler这个工具来对路由器访问的请求进行解析
3、通过Fiddler这个工具让我们了解到DI-504应用的是Basic这个认证方式
4、参考代码:
package com.van.client;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author van
*
*/
public class Router {
private static final String USERNAME_PASSWORD = "admin:a";
public Router() {
this.disconnect();
}
//http://192.168.3.1/do_cmd.htm?CMD=SYS_CONF&CCMD=0&nowait=1重启路由器
private void disconnect() {
String urlStr = "http://192.168.3.1/info.htm";
this.runCgi(urlStr, USERNAME_PASSWORD);
}
private void runCgi(String urlStr, String authorizationStr) {
URL xUrl = null;
HttpURLConnection xHuc = null;
try {
xUrl = new URL(urlStr);
if (xUrl != null) {
xHuc = (HttpURLConnection) xUrl.openConnection();
if (xHuc != null) {
if (!"".equals(authorizationStr)) {
xHuc.setRequestProperty("Authorization", "Basic "
+ Base64.encodeBytes(USERNAME_PASSWORD
.getBytes()));
}
xHuc.setRequestProperty("Content-Length", "0");
xHuc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
xHuc.connect();
String aa = xHuc.getResponseMessage();
下面这些是用来获取IP地址的
InputStream in=xHuc.getInputStream();
int chint=0;
StringBuffer sb=new StringBuffer();
while((chint=in.read())!=-1){
sb.append((char)chint);
}
String html=sb.toString();
int first=html.indexOf("wanIP=\"");
String content=html.substring(459,471);
System.out.println(content);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (xHuc != null) {
xHuc.disconnect();
}
}
}
public static void main(String[] args) {
new Router();
}
}
其中文中引用到的Base64的java文件在插件里面,还有一些API说明