2013.07.22(2)——— android 验证码与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;}





更多相关文章

  1. android手机图片查看
  2. android 图片双缓存,开源框架 universali image loader
  3. Android实现在webview中长按图片弹出菜单保存图片
  4. Android快速开发框架之xUtils---图片模块
  5. Android ImageView图片自适应大小
  6. android切换到后台图片纹理丢失的解决方案
  7. 万能imageLoader加载图片的包装,直接用
  8. 2010.12.16——— android listView 显示图片 内存溢出问题

随机推荐

  1. SaaS时代对数据安全的重新思考
  2. ORA-30009: CONNECT BY 操作内存不足--XM
  3. 让数据面临风险的十大常见云安全错误
  4. 【OCP最新题库解析(052)--题10】 Which t
  5. 避免网络***的数据存储安全优秀实践
  6. 网络安全在当今的数字营销中如此重要
  7. 敏感数据的加密方案及实现
  8. 4类危险的密码设置盲区,一起学习不要踩雷
  9. 数据保护即服务如何为组织业务提供帮助
  10. 如何将网页保存成mhtml格式