引言
这篇文章是我以前在一个论坛里写的,今天把这篇文章转移到这里。
文章主要介绍了如何模拟一个网站的登录。
这里使用的辅助工具是按键精灵,编程语言类似于VB。
实现步骤
第一步,获取登录地址
打开登录界面:

打开调试工具,点击登录按钮,查看数据信息:

从调试工具中可以看到实际登录地址和需要提交的参数。
第二步,获取验证码地址


第三步,在浏览器中正常登录,查看登录成功和失败后返回的信息(方便后边进行判断是否登录成功)
登录失败显示信息:

登录成功显示:

第四步,开始制作
首先设计界面:

代码实现:
'名称:使用WinHttp实现POST方式用户模拟登录网站
'按键ID:383810086wa
'E-mail:383810086@qq.com
'时间:2015.6.13
'-------------------------------------------
'======相关数据信息=======================================================
url_login = "http:///login.php?action=login" '网站后台登录地址 |
url_verify = "http:///yzm.php" '网站验证码地址 |
' |
form_user = "xxxxxxxxxxx" '网站用户名 |
form_passwd = "xxxxxxxxx" '网站用户密码 |
'========================================================================
'获取网站cookie
Form1.InputBox_debug.Text = "正在获取Cookie..." & vbCrlf '用于在界面上显示运行信息
Dim cookie
Set 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 With
Form1.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 & "正在获取验证码..." & vbCrlf
Dim 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 = .ResponseBody
End With
Set ObjStream = CreateObject("Adodb.Stream")
With ObjStream
.Type = 1
.Mode = 3
.Open
.Write verify_bit '写入验证码字节集
.SaveToFile ".\verify.jpg",2 '将验证码保存为本地图片
.Close
End With
Form1.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 & "请输入验证码" & vbCrlf
MessageBox "请输入验证码"
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 100
Loop
'进行网站用户登录
Form1.InputBox_debug.Text = Form1.InputBox_debug.Text & "进行登录..." & vbCrlf
Dim login_data '定义用户登录数据
Dim html_bit '定义登录页面返回字节集
login_data = "username=" & form_user & "&password=" & form_passwd & "&checknum=" & checknum
With 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 = .ResponseBody
End With
With ObjStream
.Type = 1
.Mode = 3
.Open
.Write html_bit
.Position = 0
.Type = 2
.Charset = "UTF-8"
.Close
End With
Set ObjWinHttp = Nothing
Set ObjStream = Nothing
If 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
调试结果:



|