import java.io.*;
import java.net.*;
import java.util.Scanner;
import javax.swing.*;
public class Cmcc {
//设定参数
private static String testPage;//测试页面
private String phoneNumber;//手机号码
private String password;//登陆密码
private static int checkTime;//刷新时间
private String sURL;//递交的基本参数(包含session)
private String rid;
private String vlanid;
private String rid1;
private String session;
private int errorNum = 0;//连续读取测试页面异常的次数
private static final int ERROR_NUM = 10;//连续读取测试页面异常超过此次数则退出系统
private static int outNum = 0;//连接正常时输出的数字
private URL url;
/**
* 初始化参数,如手机号码、密码
* */
public boolean init() {
try {
BufferedReader br = new BufferedReader(new FileReader("login.ini"));
try {
String ph = br.readLine();
phoneNumber = ph.substring(ph.indexOf("手机号码")+5,ph.indexOf(";"));
if(phoneNumber.length()<11){
JOptionPane.showMessageDialog(new JDialog(), "手机格式异常,请设置好配置文件再运行程序!");
System.exit(0);
}
ph = br.readLine();
password = ph.substring(ph.indexOf("登录密码")+5,ph.indexOf(";"));
if(password.length()==0) {
JOptionPane.showMessageDialog(new JDialog(), "密码没设置,请设置好配置文件再运行程序!");
System.exit(0);
}
ph = br.readLine();
checkTime = new Integer(ph.substring(ph.indexOf("检测间隔")+5,ph.indexOf(";")));
ph = br.readLine();
testPage = ph.substring(ph.indexOf("测试连接是否正常的网页")+12,ph.indexOf(";"));
br.close();
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(new JDialog(), "配置信息异常!");
System.exit(0);
}
} catch (FileNotFoundException e) {
Scanner sc = new Scanner(System.in);
System.out.println("找不到配置文件,现在开始创建!");
System.out.println("请输入登录手机号码:");
String num = sc.next();
System.out.println("请输入登录密码:");
String pass = sc.next();
String iniStr = "手机号码="+num+";\n登录密码="+pass+";\n检测间隔=10;(秒)\n测试连接是否正常的网页=http://www.baidu.com;\n\n注意:检测间隔建议设置10秒,格式请勿乱修改,出错自负";
try {
Writer wt = new FileWriter("login.ini");
BufferedWriter bw = new BufferedWriter(wt);
bw.write(iniStr);
bw.flush();
bw.close();
wt.close();
return false;
} catch (IOException e1) {}
}
return false;
}
/**
* 获取session信息并赋值给相关参数sURL
* */
public void setParameter() {
try {
// testPage = "http://www.baidu.com";
url = new URL(testPage);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"GBK"));
br.readLine();//第一行HTML信息,没用
br.readLine();//第二行HTML信息,没用
//把所读网页内容的第三行(主要信息都在这)赋值给新建的字符串body
String body = br.readLine();
//截取body中有用的信息
rid = body.substring(body.indexOf("ipm=")+5, body.indexOf("ipm=")+13);
vlanid = body.substring(body.indexOf("vlanid=")+8, body.indexOf("vlanid=")+10);
rid1 = body.substring(body.indexOf("ss1=")+5, body.indexOf("ss1=")+17);
String ss1 = body.substring(body.indexOf("ss3=")+5, body.indexOf("ss3=")+13);
String ss2 = body.substring(body.indexOf("ss4=")+5, body.indexOf("ss4=")+17);
String ss3 = body.substring(body.indexOf("ss2=")+5, body.indexOf("ss2=")+9);
session = ss1 + "-" + ss2 + "-" + ss3;
//所有信息赋值给sURL
sURL = "&rid="+rid+"&vlanid="+vlanid+"&rid1="+rid1+"&session="+session;
// System.out.println(sURL);
} catch (Exception e) {}
}
/**
* 判断测试页面是否连接成功!成功返回0,掉线返回1,异常返回2
* */
public int isConn() {
try {
URL url = new URL(testPage);
URLConnection conn = url.openConnection();
conn.getDoInput();
url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String er = "";
//读取3行信息,都没用的
br.readLine();br.readLine();br.readLine();
//第4行有用,赋值给er
er = br.readLine();
if(er.indexOf("空间")==-1) {
return 1;
}
errorNum = 0;
return 0;
} catch (Exception e) {
if(errorNum>ERROR_NUM){
System.out.println("连续"+errorNum+"次以上读取不到网络信息,系统自动退出!请确定你是否连接了CMCC网络!");
System.exit(0);
}
errorNum++;
return 2;
}
}
/**
* 登陆中国移动网络
* */
public void connCmcc() {
try {
URL url = new URL("http://218.204.219.1:8080/Control?id=2000"+sURL);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.setDoOutput(true);
// uc.setRequestMethod("post");
uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(), "8859_1");
//发送用户名密码
out.write("strAccount="+phoneNumber+"&strPassword="+password+"&");
out.flush();
out.close();
System.out.println("登陆信息发送完毕,等待回复...如果等待过久请关闭程序!");
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"GBK"));
String er = null;
while((er = br.readLine())!=null) {
if(er.indexOf("帐号或密码错误")!=-1){
JOptionPane.showMessageDialog(new JDialog(), "帐号或密码错误,请修改参数文件!");
System.exit(0);
}
}
br.close();
} catch (Exception e) {}
}
/**
* 连接流程控制,连接正常返回true,不正常返回false
* */
public boolean systemControllor(Cmcc c) {
if(!c.init())c.init();
System.out.println("初始化参数正常!");
c.setParameter();
System.out.println("获取session等连接参数正常!");
c.connCmcc();
System.out.println("成功获得系统回复,验证网络是否通过...");
while(c.isConn()!=0){
System.out.println("网络测试异常,系统重新连接!");
return false;
}
return true;
}
/**
* 系统定时刷新流程控制
* */
public void flushConn(Cmcc c) {
while(true) {
switch(c.isConn()) {
case 0:
outNum++;
if(outNum > 9)outNum = 0;
System.out.print(outNum);
break;
case 1:
System.out.print("\n垃圾中国移动,又自动掉线了!系统重新连接...\n");
while(!c.systemControllor(c))c.systemControllor(c);
// JOptionPane.showMessageDialog(new JDialog(), "掉线了~~");
break;
case 2:
System.out.print("\n我靠!网络不稳定,读不到信息");
break;
}
try {
Thread.sleep(checkTime*1000);
} catch (Exception e) {}
}
}
/**
* 主程序(废话)
* */
public static void main(String[] args) {
System.out.println("================================");
System.out.println("===CMCC自动连接系统(非UI版)===");
System.out.println("================================");
System.out.println("");
Cmcc c = new Cmcc();
while(!c.systemControllor(c))c.systemControllor(c);
System.out.println("\n测试网页"+testPage+"成功!可以正常上网了!\n");
System.out.println("系统间隔"+checkTime+"秒自动检测连接是否正常!");
System.out.println("连接正常则会输出0~9的数字,不正常会有相应提示!");
c.flushConn(c);
}
}
评论0