java 重启路由器

package com;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import com.Http;
 
public class Rotes {
    static final String kuser = "admin"; // 用户名
    static final String kpass = "admin"; // 密码
 
    static class MyAuthenticator extends Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
        }
    }
     
    /**
     * 重启路由后停顿 40 秒等待新链接
     * @throws Exception
     */
    public Rotes() throws Exception{
        boolean v = checkLogin();
        if(v){
            restart();
        }
    }
     
     
    /**
     * 重启路由
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    private Boolean restart() throws IOException, InterruptedException{
        Http h=new Http("http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7");
        String code = h.get();
        if(code.indexOf("正在重新启动")!=-1){
            System.out.println("正在重启路由,请稍等...");
            return true;
        }
        else{
            System.out.println("重启失败!");
            return false;
        }
    }
     
    /**
     * 登陆路由
     * @return
     * @throws Exception
     */
    private Boolean checkLogin() throws Exception{
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://192.168.1.1/");
        InputStream ins = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins,"gbk"));
        String str;
        String html="";
        while ((str = reader.readLine()) != null)
            html+=str;
         
        if(html.indexOf("多功能宽带路由器")!=-1){
            System.out.println("登陆成功!");
            return true;
        }else{
            System.out.println("登陆失败!");
            return false;
        }
    }
}

你可能感兴趣的:(java)