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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值