Asynchttpclient实现模拟登陆教务系统失败解决办法

        需要准备的工具:

1. android开发的工具与环境 。我是用android写的,关于这个其他的就不多说了。

2.HttpWatch+IE浏览器。需要使用HttpWatch来查看教务系统在登陆时向服务器发送了哪些数据,HttpWatch在网上搜一下就能下载,浏览器其他的应该也可以,本人只用了IE。

      下面正片开始。

    既然是模拟登陆,我们首先要知道在教务网站上点击登陆的一刹那,浏览器除了用户名,密码,验证码之外还向服务器发送了哪些参数,这个需要通过HttpWatch来查看。

      打开登陆界面,右键选择HttpWatch打开,我使用的是9.1版本,输入账号密码验证码后,点击红色的按钮Record,开始记录。






然后点击登陆按钮。会在Httpwatch中得到如下信息,根据网站的不同,信息可能会略有不同。





我的显示为Post提交,点击POST,查看下方的POSTData选项卡可以看到除了password密码,username用户名,vcode验证码以外,还有_eventId,execution,lt,service四个个参数一并提交给了服务器。



那么这四个参数怎么来的呢,通过查看登陆界面的HTML代码,我发现这四个参数在你 访问登陆界面时,服务器就在登陆界面的HTML里放置了这四个参数,如下图所示。





既然参数就在返回的HTML代码里,那我们把里面的参数值筛选出来就好了,使用Jsoup筛选的代码如下
     

private void getOther() {//获取四个参数_eventId,execution,lt,service
		final ProgressDialog dialog =CommonUtil.getProcessDialog(LoginActivity.this,"正在获取其他");
		dialog.show();
		//URL_Other是登陆界面的地址。
		HttpUtil.post(HttpUtil.URL_Other, new AsyncHttpResponseHandler() {
			
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				
				try {
					String  resultContent = new String(arg2, "gb2312");//将服务器返回的比特流按gb2311编码转换成字符串格式
					
					Toast.makeText(getApplicationContext(), "参数获取成功!!!",Toast.LENGTH_SHORT).show();
				dialog.dismiss();
				linkService.isOther(resultContent);//对字符串进行筛选,解析出我们需要的四个值
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				
				Toast.makeText(getApplicationContext(), "参数获取失败!!!",
						Toast.LENGTH_SHORT).show();
				dialog.dismiss();

			}
		});
	}

通过接受服务器返回的HTML,获取参数。
public String isOther(String arg2){//arg2是服务器返回的HTML的字符串格式  

	    Document doc = Jsoup.parse(arg2);  
	    Element form = doc.select(".dlbg").get(0);  
	    HttpUtil.lt = form.select("input[name=lt]").get(0).val();  //将筛选出的参数赋值给lt,稍后一并提交
	    HttpUtil.execution = form.select("input[name=execution]").get(0).val();  
	    HttpUtil._eventId = form.select("input[name=_eventId]").get(0).val();  
	     return null;
	  }

通过就jsoup筛选。
public static RequestParams getLoginRequestParams() {
        // 设置请求参数
        RequestParams params = new RequestParams();
        params.add("_eventId", _eventId);
        params.add("execution", execution);
        params.add("service", service);
        params.add("lt", lt);
        params.add("password", password);
        params.add("vcode", vcode);
        params.add("username", username);
        return params;
        
    }
另附获取验证码代码:

/**
     * 获得验证码
     */
    private void getCode() {
        final ProgressDialog dialog =CommonUtil.getProcessDialog(LoginActivity.this,"正在获取验证码");
        dialog.show();
        HttpUtil.get(HttpUtil.URL_CODE, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                
                InputStream is = new ByteArrayInputStream(arg2);
                Bitmap decodeStream = BitmapFactory.decodeStream(is);
                code.setImageBitmap(decodeStream);
                Toast.makeText(getApplicationContext(), "验证码获取成功!!!",Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                    Throwable arg3) {
                
                Toast.makeText(getApplicationContext(), "验证码获取失败!!!",
                        Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });
    }

登陆方法

public void login() {
		HttpUtil.username = username.getText().toString().trim();
		HttpUtil.password = password.getText().toString().trim();
		//需要时打开验证码注释
		HttpUtil.vcode = secrectCode.getText().toString().trim();
		if (TextUtils.isEmpty(HttpUtil.username)
				|| TextUtils.isEmpty(HttpUtil.password)) {
			Toast.makeText(getApplicationContext(), "账号或者密码不能为空!",
					Toast.LENGTH_SHORT).show();
			return;
		}
		final ProgressDialog dialog =CommonUtil.getProcessDialog(LoginActivity.this,"正在登录中!!!");
		dialog.show();
		RequestParams params = HttpUtil.getLoginRequestParams();// 获得请求参数
		HttpUtil.getClient().setURLEncodingEnabled(true);
		HttpUtil.post(HttpUtil.URL_LOGIN, params,
				new AsyncHttpResponseHandler() {

					@Override
					
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						try {
							String resultContent = new String(arg2,"utf-8");
							
							if(linkService.isLogin(resultContent)!=null){
								linkService.isLogin(resultContent);
								Toast.makeText(getApplicationContext(),
										"登录成功!!!", Toast.LENGTH_SHORT).show();
								jump2Main(resultContent);
								
							}else{
								Toast.makeText(getApplicationContext(),"账号或者密码错误!!!", Toast.LENGTH_SHORT).show();
							}

						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						} finally {
							dialog.dismiss();
						}
					}
					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2,
							Throwable arg3) {
						Toast.makeText(getApplicationContext(), "登录失败!!!!",
								Toast.LENGTH_SHORT).show();
						dialog.dismiss();
					}
				});
	}
新手第一次写,多提建议,不喜勿喷。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值