方法一:跟服务器开发沟通给一个接口:
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; }