import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Login {
//在此设置用户名和密码,
//另外strToIndex的内容具体参考自己的ipconfig结果,每台机器不一样
//static String strToIndex = "临时 IPv6 地址. . . . . . . . . . :";
static String strToIndex = "IP Address. . . . . . . . . . . . :";
static String username = "40850XXX";
static String password = "XXXXXX";
/**
* 函数名:main
* 功能:main函数
*/
public static void main(String[] args) {
login(username,password);
}
/**
* 函数名:login
* 功能:实现登录
*/
public static void login(String username, String password) {
Socket mySocket;
OutputStream os;
String ipv6Addr;
try {
ipv6Addr = getIpv6(); //取得ipv6地址
System.out.println(ipv6Addr);
mySocket = new Socket(InetAddress.getByName("202.204.48.82"), 80);
os = mySocket.getOutputStream();
// 要发到校园网服务器的内容处理
String str1 = "POST / HTTP/1.1\r\nHost: 202.204.48.82\r\n"
+ "Connection: keep-alive\r\nReferer: http://202.204.48.82/\r\n"
+ "Content-Length: 117\r\nCache-Control: max-age=0\r\n"
+ "Origin: http://202.204.48.82\r\n"
+ "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.77 Safari/534.24 ChromePlus/1.6.2.0\r\n"
+ "Content-Type: application/x-www-form-urlencoded\r\n"
+ "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
+ "Accept-Encoding: gzip,deflate,sdch\r\n"
+ "Accept-Language: zh-CN,zh;q=0.8\r\n"
+ "Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n"
+ "Cookie: myusername=";
//作字符串的连接处理
String content1 = str1 + username + ";pwd=" + password
+ ";username=" + username + ";smartdot=" + password
+ "\r\n\r\n";
String content2 = "DDDDD=" + username + "&upass=" + password
+ "&0MKKey=123456789&v6ip="+ipv6Addr+"&savePWD=on";
//不知道什么原因,发一次不一定会成功,脑残地设置为10次
for (int i = 0; i
os.write(content1.getBytes());
os.write(content2.getBytes());
}
//关闭输出流关闭socket
os.close();
mySocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 函数名:getIpv6
* 功能:取得校园网需要的IPV6地址
* @return
*/
public static String getIpv6() throws Exception {
String line;
String physicalAddress = "read MAC error!";
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig");
BufferedReader bd = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = bd.readLine()) != null) {
if (line.indexOf(strToIndex) != -1) {
if ((line.indexOf(":") != -1) && (line.indexOf("2001:")!=-1)) {
physicalAddress = line.substring(line.indexOf(":") + 2);
break; // 找到MAC,推出循环
}
}
}
return physicalAddress;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}