2013.07.22(2)——— android 验证码与cookie
16lz
2021-01-23
2013.07.22(2)——— android 验证码与cookie
验证码图片与cookie对应,注册时需要把验证码以及cookie传回服务器,这里就需要在获取验证码图片时 得到cookie,并且注册时传回cookie
1、得到cookie
2、传回cookie
验证码图片与cookie对应,注册时需要把验证码以及cookie传回服务器,这里就需要在获取验证码图片时 得到cookie,并且注册时传回cookie
1、得到cookie
//加载验证码private class LoadVcodeThread implements Runnable{private Handler handler;public LoadVcodeThread(Handler handler){this.handler = handler;}@Overridepublic void run() {if(Constant.isNetConnect){int count = 0;while(isRunningVcode){String url = Constant.vcode_url;HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);HttpResponse response;final String fileName = "vcode.png";String cookies = "";FileOutputStream fileOutputStream = null;try {response = client.execute(get);Header bean = response.getLastHeader("Set-Cookie");cookies = bean.getValue();HttpEntity entity = response.getEntity();InputStream is = entity.getContent();File file = null;String root = null;if (is != null) {root = UtilFile.getFolderPath(UtilFile.DIR_VERIFY); file = new File(root+"/"+fileName); if(file.exists()){ file.delete(); }fileOutputStream = new FileOutputStream(file);byte[] buf = new byte[1024];int ch = -1;while ((ch = is.read(buf)) != -1) {fileOutputStream.write(buf, 0, ch);}}fileOutputStream.flush();final String filePath = file.getAbsolutePath();BitmapFactory.Options options=new BitmapFactory.Options();options.inJustDecodeBounds=true;//只得到图片大小信息 不分配内存options.inPreferredConfig = Bitmap.Config.RGB_565;Bitmap tempBitmap=BitmapFactory.decodeFile(filePath, options);int scale=(int)(options.outWidth/UtilManager.getInstance()._utilPhone.getPxFromDip(80));options.inJustDecodeBounds=false;if(scale > 1){options.inSampleSize=scale;//设置缩放级别}tempBitmap = BitmapFactory.decodeFile(filePath, options);if(tempBitmap==null){file.deleteOnExit();if(count==3){isRunningVcode = false;handler.sendEmptyMessage(MSG_REGIST_VCODE_FAIL);}else{count++;}}else{isRunningVcode = false;Message msg = new Message();msg.what = MSG_REGIST_VCODE_SUC;Object [] tArr = {tempBitmap, cookies};msg.obj = tArr;handler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();}}}else{}}}
2、传回cookie
public static String queryStringForGet(String url, String cookies){HttpGet request = UtilHttp.getHttpGet(url);//BasicHeader tHeader = new BasicHeader("Set-Cookie", cookies);//request.setHeader(tHeader);request.setHeader("Cookie", cookies);UtilLog.log(TAG, "url: "+url + ", cookies: " + cookies);try {return getResult(request);}catch(ConnectTimeoutException e){e.printStackTrace();//queryStringForGet(url);}catch(SocketTimeoutException e){e.printStackTrace();//queryStringForGet(url);}catch (Exception e) {e.printStackTrace();}return null;}
更多相关文章
- android手机图片查看
- android 图片双缓存,开源框架 universali image loader
- Android实现在webview中长按图片弹出菜单保存图片
- Android快速开发框架之xUtils---图片模块
- Android ImageView图片自适应大小
- android切换到后台图片纹理丢失的解决方案
- 万能imageLoader加载图片的包装,直接用
- 2010.12.16——— android listView 显示图片 内存溢出问题