android开发:使用HttpURLConnection读取验证码,并且发送请求进行验证的方法

直接上代码,下面是获取验证码图片的代码:

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://192.168.1.205/verify.php?rand=29569");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();//建立链接

                    InputStream in = conn.getInputStream();//读取图片先

                    bitmap = BitmapFactory.decodeStream(in);//解码图片
                    handler.sendEmptyMessage(1);//发送消息,把图片显示出去

                    String cookieval = conn.getHeaderField("set-cookie");//读取cookies信息

                    if(cookieval!= null){
                        Log.d(TAG,cookieval);
                        sessionid = cookieval.substring(0,cookieval.indexOf(";"));
                        //这里是读取sessionid,一般是放在第一位的:Cookie:PHPSESSID=h8tcqq5h7nj9ll42u5656k0a54;
                        Log.d(TAG,sessionid);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
});
imageView.callOnClick();//启动程序的时候call点击方法

 

接着就是单击按钮提交请求了:

but1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String verity = editText.getText().toString();
        final String url1 = "http://192.168.1.205/mapi/index.php?act=send_reset_pwd_code&r_type=1&verify="+verity+"&mobile=15608309795";

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(url1);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    if(sessionid != null){
                        conn.setRequestProperty("cookie",sessionid);//把COOKIE设置进去,不然服务器没办法读取到session里的信息
                    }

                    //这里是做了一次文本处理,就是服务器返回的数据
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    String line = null;
                    String str = "";
                    while ((line = br.readLine()) != null){
                        str += line;
                    }
                    br.close();
                    Log.d(TAG,str);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
});

 

Handler handler = new Handler(){
    public void handleMessage(Message msg) {
        if (msg.what == 1){
            imageView.setImageBitmap(bitmap);
        }
    }
};

效果图片:QQ截图20160714151336

Leave a Comment

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |