java 模拟登陆正方教务系统


     比较懒,主要说一下思路。。

1、首先正方教务系统的验证码当你不去向服务器申请的时候,可以直接无视它不需要去输入验证码。可以通过浏览器屏蔽学校教务系统的图片就可以直接输入账号密码回车进去。。

2、通过抓包软件可以分析得到登录教务系统所需要的一些参数,比如账号,密码(废话),还有一些隐藏的参数需要自己去获取,例如_VIEWSTATE 这个参数,这个参数是可变所以每次登陆的时候都需要重新获取(如何获取下面会给出源码)

3、查询成绩的时候也是通过post方法发送参数去向服务器发送请求。不过这里需要注意几个问题 第一个是cookie 这个由于登录成功的时候需要保存下来以便访问其它页面,不过java里面的httpclient类提供了很大的方便,当我们用这个对象去登录成功之后会自动帮我们保存cookie 所以不需要手动保存。另一个就是重定向问题,由于教务系统会发生302跳转(通常会出现Object moved to here......),所以访问成绩的时候需要我们先设置referer这个参数,代表我们从referer跳转到成绩的页面,具体可以通过抓包查看

好像就差不多这样了 - -

我们需要用到httpclient 这个包 可以去搜一下下载


<span style="font-size:18px;">package StudentManage;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class httpclient {
	//模拟登录教务系统的网址
	private static String url = "http://******";
	//课表网址
	private static String urlkb ="http://******/xskbcx.aspx?xh=";
	//成绩网址
	private static String urlcj = "http://********/xscj_gc.aspx?xh=";
	//成绩,课表。。。的referer源地址
	private static String urlrefer = "http://********/xs_main.aspx?xh=";
	//模拟登陆时所需要的参数 通过抓包可获取具体的值
	private static String __VIEWSTATE;
	private static String Button1 = "";
	private static String hidpdrs = "";
	private static String hidsc = "";
	private static String lbLanguage = "";
	private static String RadioButtonList1 = "%D1%A7%C9%FA";
	private static String TextBox2;
	private static String txtSecretCode = "";
	private static String txtUserName;
	private static String ddlXN;
	private static String ddlXQ;
	private static String cjButton1 ="按学期查询";
	private static CloseableHttpClient client = HttpClients.createDefault();
	//设置登录的id psd
	public void set(String id, String psd){
		TextBox2 = psd;
		txtUserName = id;
		urlkb+=id;
		urlcj+=id;
		urlrefer+=id;
	}
	//查询成绩的年份
	public void setcj(String year,String tern){
		ddlXN=year;
		ddlXQ=tern;
//		System.out.println(ddlXN);
//		System.out.println(ddlXQ);
	}
	//获取 __VIEWSTATE 总共两次获取 一次登录的时候 一次为获取成绩的时候
	private static void Getu(String ur,int val) throws ClientProtocolException, IOException {
		//先用get方法模拟登录ur
		HttpGet httpget = new HttpGet(ur);
		if(val==1)
			httpget.setHeader("Referer",urlrefer); //第二次登陆的时候需要添加源地址防止出现302跳转
		//利用client发送请求放回一个Httprespond的类型
        HttpResponse re2 = client.execute(httpget);
        //利用jsoup的类将response.getEntity获取得到html代码变成Doc文本
        Document doc = Jsoup.parse(EntityUtils.toString(re2.getEntity(), "utf-8")); //jsoup 也可以去找相应的包
        //查找 __VIEWSTATE这个的值
        __VIEWSTATE = doc.select("input[name=__VIEWSTATE]").val();
        //释放get
        httpget.abort();
	}
	//模拟登录
	public int Login() throws ClientProtocolException, IOException {
		//获取 __VIEWSTATE的值
		Getu(url,0);
		//通过post方法模拟登陆
		HttpPost post = new HttpPost(url);
		//post所需的参数存放到ArrayList
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
		list.add(new BasicNameValuePair("Button1", Button1));
		list.add(new BasicNameValuePair("hidpdrs", hidpdrs));
		list.add(new BasicNameValuePair("hidsc", hidsc));
		list.add(new BasicNameValuePair("lbLanguage", lbLanguage));
		list.add(new BasicNameValuePair("RadioButtonList1", RadioButtonList1));
		list.add(new BasicNameValuePair("TextBox2", TextBox2));
		list.add(new BasicNameValuePair("txtSecretCode", txtSecretCode));
		list.add(new BasicNameValuePair("txtUserName", txtUserName));
		//将参数通过post方法请求服务器
        post.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = client.execute(post);
        //释放post
        post.abort();
        //当登陆返回码为302的时候登陆成功
        if(response.getStatusLine().getStatusCode() == 302)
        	return 1;
        else
        	return 0;
	}
	//获取课表
	public Document  Getkb() throws ClientProtocolException, IOException {
		//获取课表通过get方法
        HttpGet get = new HttpGet(urlkb);
        //设置referer 防止出现302跳转
        get.setHeader("Referer",urlrefer);
        HttpResponse res = client.execute(get);
        Document doc = Jsoup.parse(EntityUtils.toString(res.getEntity(), "utf-8"));
        get.abort();
        //返回document文件
		return doc;
	}
	//获取成绩
	public Document Getcj() throws ClientProtocolException, IOException {
		//__VIEWSTATE
        Getu(urlcj,1);
        //System.out.println(__VIEWSTATE);
        //参数
        ArrayList<NameValuePair> par = new ArrayList<NameValuePair>();
		par.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
		par.add(new BasicNameValuePair("Button1", cjButton1));
		par.add(new BasicNameValuePair("ddlXN",ddlXN));
		par.add(new BasicNameValuePair("ddlXQ",ddlXQ));
		//post方法访问成绩url
		HttpPost g = new HttpPost(urlcj);
		//传参数
		g.setEntity(new UrlEncodedFormEntity(par));
		//设置urlrefer
		g.setHeader("Referer",urlrefer);
		HttpResponse r = client.execute(g);
		Document doc = Jsoup.parse(EntityUtils.toString(r.getEntity(), "utf-8")); 
		return doc;
	}
}</span>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值