博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动化测试 短信验证登录
阅读量:4691 次
发布时间:2019-06-09

本文共 2814 字,大约阅读时间需要 9 分钟。

方法一:跟服务器开发沟通给一个接口:

import requests, json
r = requests.get(url='http://test-capha.singer.com/c/13524273413 
')  # 带参数的GET请求
verifycode = eval(r.text)[1]['verifycode']
print(verifycode)
 
方法二,在网上找到的方法记录下:
参考链接:  http://www.jb51.net/article/89747.htm,之前试的发现只保存短信的后面一部分,后来才发现是前面一排字被挡住了。。。。。。。
分两步:第一步在手机端安装一个apk,保存手机的短信到txt文档里面:
public class SMSReceiver extends BroadcastReceiver {
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent){ if (intent.getAction().equals(SMS_RECEIVED_ACTION)){ SmsMessage[] messages = getMessagesFromIntent(intent); String verifyCode=""; for (SmsMessage message : messages){ verifyCode += message.getDisplayMessageBody(); } writeFile(verifyCode);//将短信内容写入SD卡 } } public final SmsMessage[] getMessagesFromIntent(Intent intent){ Object[] messages = (Object[]) intent.getExtras().get("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; } //将短信内容写到SD卡上的文件里,便于将文件pull到PC,这样可方便其它如WWW/WAP平台的自动化 @SuppressLint("SdCardPath") public void writeFile(String str){ String filePath="/mnt/sdcard/verifyCode.txt"; byte [] bytes = str.getBytes(); try{ File file=new File(filePath); file.createNewFile(); FileOutputStream fos=new FileOutputStream(file); fos.write(bytes); fos.close(); }catch(IOException e){ e.printStackTrace(); } } } 注册:
receiver android:name=".SMSReceiver">     
权限:
打包apk后安装 第二步:使用adb 命令读取txt文档内容,再从中获取短信 C# //从手机文件中获取短信 public String getSmsText() { Process process = new Process(); String command = "adb shell cat /mnt/sdcard/verifyCode.txt"; string outtr = ""; process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/C" + command; process.StartInfo.RedirectStandardInput = true; //重定向输入(一定是true) process.StartInfo.RedirectStandardOutput = true; //重定向输出 process.StartInfo.RedirectStandardError = true; process.StartInfo.UseShellExecute = false; try { process.Start(); outtr = process.StandardOutput.ReadToEnd(); process.WaitForExit(5000); }catch(Exception e) { Console.WriteLine(e.Message); }finally { if(process != null) process.Close(); } Console.WriteLine("开始读取内容" + outtr); return outtr; } //从短信中提取验证码 public String getVerificationCode(String s) { MatchCollection mc = Regex.Matches(s, @"\d{6}"); String str = ""; int k = 0; foreach (Match item in mc) { if (k == 1) break; Console.WriteLine(item.Value); str = item.Value; k++; } return str; }

转载于:https://www.cnblogs.com/zjxyz2008zhangjuan/p/7279337.html

你可能感兴趣的文章
也谈WebKit、Gecko使用图形库
查看>>
Could not write file: C:\......\.classpath
查看>>
css里关于浏览器的前缀
查看>>
HDU - 1175 连连看 DFS (记录方向)
查看>>
重头开始学23种设计模式:单例模式
查看>>
mepg
查看>>
C primer Plus 作业第四章
查看>>
combobox 下拉框 高度 调节 呵呵
查看>>
数据库监控--12c Enterprise Manager配置
查看>>
webview加载页面为什么在UI线程里面做,难道不是耗时操作么
查看>>
adb server is out of date.killing
查看>>
JS 将json数组转为嵌套层级数组
查看>>
【Java_Spring】RestTemplate发HTTP请求详解
查看>>
宏的方式显示ALV
查看>>
数据库设计三大范式的理解
查看>>
20180702小测
查看>>
13个 ASP.NET MVC 的扩展
查看>>
Navicat Premium 连接MySQL数据库出现Authentication plugin 'caching_sha2_password' cannot be loaded的解决方案...
查看>>
bzoj3527 [Zjoi2014]力
查看>>
漫谈:机器学习中距离和相似性度量方法
查看>>