CMCC-EDU登陆器

RT。由于在使用CMCC-EDU进行上网时,在网页里登陆极其慢,

所以昨天花半天时间实现了一个CMCC-EDU登陆器。

思路就是,从浏览器先手工登陆抓包,分析整个登陆的过程,然后用程序去模拟。

针对每一个Get 和Post 分析其带的参数,提交到的地址,模拟

程序使用了httpcomponents-client-4.3.jar、和Jsoup-1.7.2.jar第三方类库。



PS:1.试了好多遍打jar文件,但是都运行不了,实在是没耐心了,有哪个网友知道怎么弄麻烦告知下。。。

  2.显示用了多久时间的还没弄好。回头再弄吧。!


上代码


1.界面程序

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;


public class MainFrame extends JFrame{

	/**
	 * @param args
	 */
	public static String name="";
	public static String password="";
	private static CookieStore cookieStore=new BasicCookieStore();
	CMCC cmcc;
	JTextField textName,textPassword;
	JButton btStart,btEnd;
	JLabel labelName;
	JLabel labelEmail;
	
	public MainFrame(){
		setSize(220,200);
		setTitle("CMCC-EDU登陆器  by Sumrise");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textName=new JTextField("账号",10);
		textPassword=new JTextField("密码",10);
		labelName=new JLabel("CMCC登陆器 by Sumrise");
		labelEmail=new JLabel("联系方式:sumrise@vip@qq.com");
		setLayout(new FlowLayout());
		add(textName);
		add(textPassword);
		add(labelName);
		add(labelEmail);
		btStart=new JButton("登陆");
		btEnd=new JButton("下线");
		add(btStart);
		add(btEnd);
		setVisible(true);
		
		btStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(! textName.getText().trim().equals("账号"))
					name=textName.getText().trim();
				if(! textPassword.getText().trim().equals("密码"))
					password=textPassword.getText().trim();
				
				JOptionPane.showMessageDialog(null, "正在登陆请稍等几秒");
				cmcc=new CMCC(MainFrame.this);
				new Thread(cmcc).start();
			}
		});
		btEnd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cmcc.unload();
			}
		});
		textName.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(textName.getText().trim().equals("账号"))
					textName.setText("");
			}
		});
		textPassword.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(textPassword.getText().trim().equals("密码"))
					textPassword.setText("");
			}
		});
		
	}
	public static void main(String[] args) {
		new MainFrame();
	}

}


2.Web获取页面小框架

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


public class WebGetPage {
	
	HttpClient client;
	CookieStore cookieStore;
	boolean isUseCookies=false;
	public WebGetPage() {
		client=new DefaultHttpClient();
		cookieStore=new BasicCookieStore();
	}
	
	/**
	 * @param isUseCookies 是否使用Cookie
	 * */
	public WebGetPage(boolean isUseCookies) {
		this();
		this.isUseCookies=isUseCookies;
	}
	public String postPage(String url,Map<String, String> map){
		return postPage(url,map,"UTF-8");
	}
	
	public String postPage(String url,Map<String, String> map,String charset){
		return postPage(url, map, charset, isUseCookies);
	}
	public String postPage(String url,Map<String, String> map,String charset,boolean isUseCookies){
		HttpClient client=new DefaultHttpClient();
		HttpPost httpPost=new HttpPost(url);
		HttpResponse response=null;
		String respResult="";
		
		httpPost.addHeader("User-Agent","Mozilla/4.0(compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727");
		//添加Post参数
		List<NameValuePair> parameters=new ArrayList<NameValuePair>();
		Iterator<String> keyIterator=map.keySet().iterator();
		while(keyIterator.hasNext()){
			String key=keyIterator.next();
			String value=map.get(key);
			parameters.add(new BasicNameValuePair(key, value));
		}
		
		try {
			HttpEntity postBodyEnt=new UrlEncodedFormEntity(parameters,"utf-8");
			httpPost.setEntity(postBodyEnt);
	
			if(isUseCookies)
				response=client.execute(httpPost,getCookieContext());
			else
				response=client.execute(httpPost);
			respResult=EntityUtils.toString(response.getEntity(),charset);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return respResult;
	}

	
	/**
	 * Get取得页面
	 * @param url GET URL的地址
	 * @param charset 编码
	 * @param isUseCookie 是否使用Cookie
	 * */
	public String getPage(String url,String charset,boolean isUseCookie){
		HttpGet httpGet=new HttpGet(url);
		httpGet.addHeader("User-Agent","Mozilla/4.0(compatible; MSIE 7.0;Windows NT 5.1; .NET CLR 2.0.50727");
		HttpResponse response;
		String respResult="";
		try {
			if(isUseCookie)
				response=client.execute(httpGet,getCookieContext());
			else
				response = client.execute(httpGet);
			respResult=EntityUtils.toString(response.getEntity(),charset);
//			System.out.println(respResult);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return respResult;
	}
	public String getPage(String url,String charset){
		return getPage(url, charset,isUseCookies);
	}

	public String getPage(String url){
		return getPage(url,"UTF-8");
	}
	
	/**
	 * 取得Cookie并存取cookie
	 * */
	public HttpContext getCookieContext(){
		HttpContext context=new BasicHttpContext();
		context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
		return context;
	}
}


3.核心实现

package com.main;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


import javax.swing.JOptionPane;


import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class CMCC implements Runnable {
	public static String url1 = "http://211.137.133.5/?wlanacname=0039.0029.290.00&wlanuserip=";
	public static String url2 = "&ssid=CMCC-EDU&vlan=3061";
	public static String LOAD_PAGE_URL = "http://211.137.133.5/style/default_cmsn/index.jsp?paramStr=";
	public static String LOAD_POST_URL = "https://211.137.133.5/authServlet";
	public static String UNLOAD_POST_URL = "https://211.137.133.5/logoutServlet";
	public static String username = "";
	public static String password = "";


	public static String UNLOAD_PARAMSTR;


	public boolean isLoad;
	
	Document doc;
	Elements elements;
	WebGetPage web;
	
	/**拥有主页面的引用*/
	MainFrame frame;
	public CMCC() {
		web = new WebGetPage(true);
	}
	public CMCC(MainFrame frame){
		this();
		this.frame=frame;
		username=MainFrame.name;
		password=MainFrame.password;
	}


	public void run() {
		JOptionPane.showMessageDialog(null, "开始登陆了!");
		String url=url1+this.getLocalIp()+url2;
		try {
			login(url);
		} catch (ClientProtocolException e) {
			JOptionPane.showMessageDialog(null, "网络异常了!");
			e.printStackTrace();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "网络异常了!");
			e.printStackTrace();
		}
		JOptionPane.showMessageDialog(null, "登陆结束");
	}


	/**
	 * 1.访问任意一个网页拿到跳转信息 2.拿到跳转信息后,访问页面登陆页面 3.拿到登陆页面信息,登陆post
	 * 4.登陆成功后拿到登陆信息,提示上线成功 5.程序中点击下线,取得参数post退出页面 6.退出
	 * @param url 
	 * */
	/**
		 * 登陆过程
	 * */
	private void login(String url) throws ClientProtocolException, IOException {
		if(isLoad){
			System.out.println("已经登陆,请勿重复登陆");
			return;
		}
		String respResult=web.getPage(url);
//		System.out.println(respResult);
	
		/** paramStr1参数*/
		String paramStr=respResult.substring(respResult.indexOf("paramStr")+9);
		paramStr=paramStr.substring(0,paramStr.indexOf("\""));
//		System.out.println(paramStr);
		
		LOAD_PAGE_URL+=paramStr;
		respResult=web.getPage(LOAD_PAGE_URL);
//		System.out.println(respResult);
		
		doc=Jsoup.parse(respResult);
		elements=doc.getElementsByTag("form");
		Element e=elements.get(0);
//		System.out.println(e);
		String input=e.toString().substring(e.toString().indexOf("id=\"paramStr\""));
		String paramStr2=input.substring(21,input.indexOf("\" />"));
		
//		System.out.println(paramStr2);
		
		//登录Post
		Map<String, String> map=new HashMap<String, String>();
		map.put("UserName", username);
		map.put("PassWord", password);
		map.put("UserType", "1");
//		map.put("UserType", "");//
		map.put("paramStr", paramStr2);
		map.put("province", "");
		map.put("pwdType", "1");
		map.put("verifycode1", "");
		String loadResult=web.postPage(LOAD_POST_URL,map);
//System.out.println(loadResult);
		//结果返回一个302 和一个地址
		//对结果进行解析,提取出地址进行Get
		doc=Jsoup.parse(loadResult);
		elements=doc.getElementsByTag("a");
		e=elements.get(0);
		String str=e.toString();
		str=str.substring(str.indexOf("https"));
		str=str.substring(0,str.indexOf("\">"));
		
//		System.out.println(str);
		
		String loadSuccessPage=web.getPage(str);
//		System.out.println(loadSuccessPage);
		
		//登陆成功解析出用了多久了。
		
		//下线参数获取
		doc=Jsoup.parse(loadSuccessPage);
		elements=doc.getElementsByTag("form");
		e=elements.get(0);
//		System.out.println(e);
		input=e.toString().substring(e.toString().indexOf("id=\"paramStr\""));
		UNLOAD_PARAMSTR=input.substring(21,input.indexOf("\" />"));
		
//		System.out.println(UNLOAD_PARAMSTR);
		
		System.out.println("登陆成功");
		isLoad=true;
		JOptionPane.showMessageDialog(null, "登陆成功");
		//下线Post
//		unload();
		
	}
	
	
	
	/**
	 * 下线
		 * */
	public void unload(){
		if(!isLoad){
			System.out.println("没有登陆前,下个毛线?");
			JOptionPane.showMessageDialog(null, "没有登陆前,下个毛线?");
			return;
		}
		Map<String, String> unloadMap=new HashMap<String, String>();
		unloadMap.put("bOffline", "true");
		unloadMap.put("paramStr", UNLOAD_PARAMSTR);
		String unLoadPage=web.postPage(UNLOAD_POST_URL, unloadMap);
//		System.out.println(unLoadPage);
		
		doc=Jsoup.parse(unLoadPage);
		elements=doc.getElementsByTag("a");
		Element e=elements.get(0);
		String str=e.toString();
		str=str.substring(str.indexOf("https"));
		str=str.substring(0,str.indexOf("\">"));
		
//		System.out.println(str);
		
		String unLoadSuccessPage=web.getPage(str);
//		System.out.println(unLoadSuccessPage);
		
		if(unLoadSuccessPage.indexOf("下线成功")>-1){
			System.out.println("下线成功");
			isLoad=false;
			JOptionPane.showMessageDialog(null, "下线成功");
		}
	}
	




	
	/**
	 * 取得上CMCC-EDU所用的无线网卡 IP地址
	 * */
	public String getLocalIp(){
		StringBuffer sb=new StringBuffer();
		Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
			while(interfaces.hasMoreElements()){
				NetworkInterface ni=interfaces.nextElement();
				sb.append(ni.toString());
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
		String string=sb.toString();
		sb=null;
		
		String s=string.substring(string.indexOf("10.8."));
		s=s.substring(0,s.indexOf(";"));
//		System.out.println(s);
		
		return s;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值