1. 程式人生 > >android session的使用(圖片驗證碼)

android session的使用(圖片驗證碼)

功能:android端要實現圖片驗證碼功能。
這裡寫圖片描述

1、方法一:本地實現圖片驗證

在本地實現圖片驗證碼:這種方式,是不與伺服器驗證的,驗證碼在android端生成,並且自已校驗。

2、方法二:接收伺服器驗證圖片,提交時伺服器校驗。

這種方法,我們只需要有一個接受驗證碼的介面,傳過來的是一張驗證碼的圖片,當登陸的時候,把圖片中的數字傳送給伺服器,伺服器在校驗後,返回結果。

在這裡主要介紹一下方法二的使用,並且方法二用到cookie和session的相關知識。
流程如下:
這裡寫圖片描述

下面從示例中分析(用okhttp作網路請求,與httpurlconnection,httpclien原理一樣):
1、請求驗證碼介面中從響應頭在列印資訊

   Headers headers = response.headers();
                Log.d(TAG, "header " + headers);
                List<String> cookies = headers.values("Set-Cookie");
                String session = cookies.get(0);
                Log.d(TAG, "onResponse-size: " + cookies);
                s = session.
substring(0, session.indexOf(";")); Log.i(TAG, "session is :" + s);

這裡寫圖片描述

2、當驗證的時候,把JSESSIONID=3BEF5AE746C8573FF6BA3CFBC6DFF632取出並新增到請求頭中:

    Request request = new Request.Builder()
                        .addHeader("cookie",session)

下面是程式碼:

public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
private EditText text; // 請求圖片的url private String url = "http://xxx"; private final static String TAG = "MainActivity"; private OkHttpClient mOkHttpClient; private ImageView image; private Button button; private String code; private String s; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, "onCreate"); text = (EditText) findViewById(R.id.text); image = (ImageView) findViewById(R.id.image); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); image.setOnClickListener(this); getPicture(); } private void getPicture() { mOkHttpClient = new OkHttpClient(); //建立一個Request final Request request = new Request.Builder() .url(url) .build(); //new call Call call = mOkHttpClient.newCall(request); //請求加入排程 call.enqueue(new Callback() { //失敗的回撥 @Override public void onFailure(Call call, IOException e) { Log.i(TAG, "errro"); } //成功的回撥 @Override public void onResponse(Call call, Response response) throws IOException { //重新整理ui,okhttp網路請求後,不是在主執行緒中,如果要重新整理ui,必須的主執行緒中; if (response.isSuccessful()) { InputStream is = response.body().byteStream(); Bitmap bm = BitmapFactory.decodeStream(is); // // image.setImageBitmap(bm); mHandler.sendEmptyMessage(0); Message msg = new Message(); msg.obj = bm;//可以是基本型別,可以是物件,可以是List、map等; mHandler.sendMessage(msg); } Headers headers = response.headers(); Log.d(TAG, "header " + headers); List<String> cookies = headers.values("Set-Cookie"); String session = cookies.get(0); Log.d(TAG, "onResponse-size: " + cookies); s = session.substring(0, session.indexOf(";")); Log.i(TAG, "session is :" + s); } }); } Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: //完成主介面更新,拿到資料 Bitmap bm = (Bitmap) msg.obj; image.setImageBitmap(bm); break; default: break; } } }; @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: Log.i(TAG,"發這的s是: "+s); code = text.getText().toString().trim(); String pass = Md5Utils.getMD5Str("123456"); OkHttpClient client = new OkHttpClient(); FormBody body = new FormBody.Builder() .add("param.account", "admin") .add("param.password", pass) .add("param.type", "0") .add("param.code", code) .build(); Request request = new Request.Builder() .addHeader("cookie",s) //提交驗證 的url; .url("http://xxx) .post(body) .build(); Call call2 = mOkHttpClient.newCall(request); //請求加入排程 call2.enqueue(new Callback() { //失敗的回撥 @Override public void onFailure(Call call, IOException e) { Log.i(TAG, e.getMessage()); } //成功的回撥 @Override public void onResponse(Call call, Response response) throws IOException { //重新整理ui,okhttp網路請求後,不是在主執行緒中,如果要重新整理ui,必須的主執行緒中; if (response.isSuccessful()) { Log.i(TAG, response.body().string()); } Headers headers = response.headers(); Log.d(TAG, "header " + headers); } }); break; case R.id.image: getPicture(); break; } } }