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;
}
分享到:
相关推荐
基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj
2,jdk-170.tar.gz ——————————JDK1.7deb包 3,switch_java.sh -------------------------java其它版本切换 4,check_java.sh———————————版本核对 注意:使用命令接口切换时,需要将自己配置的...
python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...
微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+...
python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ...
现场评定检查表——建筑类别与耐火等级表.docx
2020.4.11-措施项目——模板工程量计算规则
22. 多——少 23. 古——今 24. 入——出 25. 穷——富 26. 轻——重 27. 细——粗 28. 快——慢 29. 爱——恨 30. 哭——笑 31. 这——那 32. 分——合 33. 圆——方 34. 远——近 35. 直——弯 36. 高——低 37. ...
大创项目——区块链追溯系统.zip大创项目——区块链追溯系统.zip大创项目——区块链追溯系统.zip大创项目——区块链追溯系统.zip大创项目——区块链追溯系统.zip大创项目——区块链追溯系统.zip大创项目——区块链...
———————————————— 版权声明:本文为CSDN博主「MechMaster」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
Android support.v7包
Android中文翻译组——Android中文API合集(4).chm
应用层包过滤防火墙 一个简单的应用层包过滤防火墙实现 Qt/SQLite/NetFilter 编译过程可使用QtCreator编译,也可使用命令行 sudo qmake && make 来编译。 运行则执行 sudo ./firewall 即可。
W3School离线手册(2015.07.17) W3School离线手册更新: 该文档为本人于2015年7月17日下午从W3School官网下载整理制作的CHM电子书文档。 1)要是有些人碰到chm打开空白的(因为chm未解锁,通常可以打开chm但显示无法...
基于IntraWeb的数据表格的多选实现 既可以单条操作,也可以多选操作。 delphi源代码。 BS开发Web网站开发,不需要安装服务器,Apache和IIS都不需要,自带企业级服务器。...————————————————
第4 章 Linux——App 后台应用最广泛的系统 .. 107 第5 章 Nginx——App 后台HTTP 服务的利器 . 126 第6 章 MySQL——App 后台最常用的数据库 .. 140 第7 章 Redis——App 后台高性能的缓存系统 . 160 第8 章 ...
直流-直流变换器的matlab仿真模块,实现了直流降压的功能。
【标题】与【描述】提及的是关于小学语文学习中的一个重要组成部分——歇后语。歇后语是中国传统文化中的一种独特语言现象,它结合了形象生动的比喻和巧妙的谐音,通常用于表达幽默、讽刺或者寓意深刻的观点。这些歇...
Android开发者指南(2) —— aapt - 农民伯伯 - 博客园.mht351.20 KB Android开发者指南(3) —— Other Tools - 农民伯伯 - 博客园.mht362.93 KB Android开发者指南(4) —— Application Fundamentals - 农民伯伯 - ...