`

2013.07.22(2)——— android 验证码与cookie

 
阅读更多
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;
		}
		
		@Override
		public 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;
	}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics