这篇文章是我以前在一个论坛里写的,今天把这篇文章转移到这里。
文章主要介绍了如何模拟一个网站的登录。
这里使用的辅助工具是按键精灵,编程语言类似于VB。
打开登录界面:
打开调试工具,点击登录按钮,查看数据信息:
从调试工具中可以看到实际登录地址和需要提交的参数。
登录失败显示信息:
登录成功显示:
首先设计界面:
代码实现:
'名称:使用WinHttp实现POST方式用户模拟登录网站'按键ID:383810086wa'E-mail:383810086@qq.com'时间:2015.6.13'-------------------------------------------'======相关数据信息=======================================================url_login = "http://xxx.com/login.php?action=login" '网站后台登录地址 |url_verify = "http://xxx.com/yzm.php" '网站验证码地址 |' |form_user = "xxxxxxxxxxx" '网站用户名 |form_passwd = "xxxxxxxxx" '网站用户密码 |'========================================================================'获取网站cookieForm1.InputBox_debug.Text = "正在获取Cookie..." & vbCrlf '用于在界面上显示运行信息Dim cookieSet ObjWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")With ObjWinHttp .SetTimeouts 0,0,0,0 .Open "GET", url_login, False .SetRequestHeader "Content-Type","application/x-www-form-urlencoded" .Send cookie = .getResponseHeader("Set-Cookie") End WithForm1.InputBox_cookie.Text = cookie '将cookie写入界面中的InputBox_cookie输入框,方便查看和读取Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "获取cookie完成" & vbCrlf'获取网站验证码Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "正在获取验证码..." & vbCrlfDim verify_bit '定义验证码字节集With ObjWinHttp .SetTimeouts 0,0,0,0 .Open "GET", url_verify, False .SetRequestHeader "Content-Type","application/x-www-form-urlencoded" .SetRequestHeader "Cookie", cookie '提交cookie .Send verify_bit = .ResponseBodyEnd WithSet ObjStream = CreateObject("Adodb.Stream")With ObjStream .Type = 1 .Mode = 3 .Open .Write verify_bit '写入验证码字节集 .SaveToFile ".\verify.jpg",2 '将验证码保存为本地图片 .CloseEnd WithForm1.InputBox_debug.Text = Form1.InputBox_debug.Text & "获取验证码成功" & vbCrlf'手动验证验证码Form1.PictureBox_Verify.Picture = ".\verify.jpg" '在界面中显示验证码Form1.Button_Verify.Visible = True '显示“确认验证码”按钮Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "请输入验证码" & vbCrlfMessageBox "请输入验证码"Dimenv IsVerifyDone '定义是否已填入验证码Dim checknum '定义接收验证码变量IsVerifyDone = False '默认 没有输入验证码Do '用于检测是否输入完成验证码 If IsVerifyDone Then Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "验证码已输入" & vbCrlf checknum = Form1.InputBox_Verify.Text Exit Do End If Delay 100Loop'进行网站用户登录Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "进行登录..." & vbCrlfDim login_data '定义用户登录数据Dim html_bit '定义登录页面返回字节集login_data = "username=" & form_user & "&password=" & form_passwd & "&checknum=" & checknumWith ObjWinHttp .SetTimeouts 0,0,0,0 .Open "POST", url_login, False .SetRequestHeader "Content-Type","application/x-www-form-urlencoded" .SetRequestHeader "Cookie", cookie '提交cookie .SetRequestHeader "Content-Length", Len(login_data) '提交数据长度 .Send login_data '数据提交 html_bit = .ResponseBodyEnd WithWith ObjStream .Type = 1 .Mode = 3 .Open .Write html_bit .Position = 0 .Type = 2 .Charset = "UTF-8" .CloseEnd WithSet ObjWinHttp = NothingSet ObjStream = NothingIf Instr(1, ObjStream.ReadText, "成功", 1) > 0 Then '验证登录后,网站页面返回的数据,查看是否登录成功 Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "登录成功!" & vbCrlf MessageBox "登录成功!"Else Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "登录失败!请检测验证码、用户名、密码是否正确!" & vbCrlf MessageBox "登录失败!请检测 用户名、密码、验证码 是否填写正确!"End If
调试结果:
联系客服