天天看點

Asynchttpclient實作模拟登陸教務系統失敗解決辦法

        需要準備的工具:

1. android開發的工具與環境 。我是用android寫的,關于這個其他的就不多說了。

2.HttpWatch+IE浏覽器。需要使用HttpWatch來檢視教務系統在登陸時向伺服器發送了哪些資料,HttpWatch在網上搜一下就能下載下傳,浏覽器其他的應該也可以,本人隻用了IE。

      下面正片開始。

    既然是模拟登陸,我們首先要知道在教務網站上點選登陸的一刹那,浏覽器除了使用者名,密碼,驗證碼之外還向伺服器發送了哪些參數,這個需要通過HttpWatch來檢視。

      打開登陸界面,右鍵選擇HttpWatch打開,我使用的是9.1版本,輸入賬号密碼驗證碼後,點選紅色的按鈕Record,開始記錄。

Asynchttpclient實作模拟登陸教務系統失敗解決辦法

然後點選登陸按鈕。會在Httpwatch中得到如下資訊,根據網站的不同,資訊可能會略有不同。

Asynchttpclient實作模拟登陸教務系統失敗解決辦法

我的顯示為Post送出,點選POST,檢視下方的POSTData頁籤可以看到除了password密碼,username使用者名,vcode驗證碼以外,還有_eventId,execution,lt,service四個個參數一并送出給了伺服器。

那麼這四個參數怎麼來的呢,通過檢視登陸界面的HTML代碼,我發現這四個參數在你 通路登陸界面時,伺服器就在登陸界面的HTML裡放置了這四個參數,如下圖所示。

Asynchttpclient實作模拟登陸教務系統失敗解決辦法

既然參數就在傳回的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();
					}
				});
	}
           

新手第一次寫,多提建議,不喜勿噴。