ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: 猎人seo

[求助] winHttp post登陆的问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2015-1-30 12:34 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
浮华、缠绕指尖 发表于 2015-1-29 17:46
唉,还是扔个附件吧。。

学习了,多谢分享!
登录商都.zip (52.8 KB, 下载次数: 80)

TA的精华主题

TA的得分主题

发表于 2015-2-2 08:44 | 显示全部楼层
本帖最后由 VBA万岁 于 2015-2-2 08:47 编辑
VBA万岁 发表于 2015-1-30 12:34
学习了,多谢分享!


奇怪,在另一台电脑上下载第10、11楼的附件,居然提示“有病毒”,遂贴出全部代码,看看是否有人帮忙诊断一下:
模块代码:
Sub test()
    MainFrm.Show
End Sub

Sub test2()
    LoginFrm.Show
End Sub


窗体MainFrm代码:
Option Explicit
Dim winHttp, temp

Private Sub btnLogin_Click()
    If Me.txtPassword = "" Or Me.txtUserName = "" Or Me.txtCheck = "" Then
        MsgBox "参数不全,无法登陆", vbCritical, "温馨提示"
    Else
        With winHttp
            .Open "POST", "http://bbss.shangdu.com/Login/login.jsp", False
            .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send "backurl=http%3A%2F%2Fbbss.shangdu.com%2FLogin%2Fchecklogin.jsp&action=newlogin&loginby=0&logintype=0&txtNAME=" & Me.txtUserName.Text & "&txtPassword=" & Me.txtPassword.Text & "&safecode=&randomcode=" & Me.txtCheck & "&x=44&y=14"
            If InStr(.responsetext, Me.txtUserName.Text) Then
                MsgBox "登录成功"
            Else
                MsgBox "登录失败"
            End If
        End With
    End If
End Sub
Private Sub Image1_Click()
    RefreshPicture
End Sub
Private Sub UserForm_Initialize()
    Me.txtUserName.Text = "seo123www": Me.txtPassword.Text = "qazwsx123"
    RefreshPicture
End Sub
Sub RefreshPicture()
    Set winHttp = CreateObject("winhttp.winhttprequest.5.1")
    temp = ThisWorkbook.Path & "\" & "tem.jpg"
    With winHttp
        .Open "GET", "http://bbss.shangdu.com/Login/randomcode.jsp?r=" & Int(1000 * Rnd), False
            .send
        ByteToFile .responsebody, temp
    End With
    Me.Image1.Picture = LoadPicture(temp)
    Me.Repaint: Kill temp
End Sub
Sub ByteToFile(ByVal arrByte, ByVal strFileName As String)
    With CreateObject("Adodb.Stream")
        .Type = 1 'adTypeBinary
        .Open
        .Write arrByte
        .SaveToFile strFileName, 2 'adSaveCreateOverWrite
        .Close
    End With
End Sub


窗体LoginFrm代码:
Option Explicit
Dim winHttp, temp

Private Sub UserForm_Initialize()
    Dim i&
    For i = 1 To 26
        ComboBox1.AddItem Sheets("原始数据").Cells(1, i).Value
    Next

    RefreshPicture
End Sub

Private Sub Image1_Click()
    RefreshPicture
    txtCheck.Value = ""
    txtCheck.SetFocus
End Sub

Sub RefreshPicture()
    Set winHttp = CreateObject("winhttp.winhttprequest.5.1")
    temp = ThisWorkbook.Path & "\" & "tem.jpg"
    With winHttp
        .Open "GET", "......", False
            .send
        ByteToFile .responsebody, temp
    End With
    Me.Image1.Picture = LoadPicture(temp)
    Me.Repaint: Kill temp
End Sub

Sub ByteToFile(ByVal arrByte, ByVal strFileName As String)
    With CreateObject("Adodb.Stream")
        .Type = 1 'adTypeBinary
        .Open
        .Write arrByte
        .SaveToFile strFileName, 2 'adSaveCreateOverWrite
        .Close
    End With
End Sub

Private Sub ComboBox1_Change()
    Dim i%, j%
    ComboBox2.Clear
    For i = 1 To 26
        If Sheets("原始数据").Cells(1, i).Value = ComboBox1.Value Then Exit For
    Next i
    For j = 2 To Sheets("原始数据").Cells(100, i).End(xlUp).Row
        ComboBox2.AddItem Sheets("原始数据").Cells(j, i).Value
    Next j
End Sub

Private Sub ComboBox2_Change()
    txtUserName.Text = pinyin(ComboBox2.Value, "", 2)
End Sub

Private Sub btnLogin_Click()
Dim URL, t
If txtUserName = "" Or Me.txtCheck = "" Then
    MsgBox "参数不全,无法登陆", vbCritical, "温馨提示"
Else
    With winHttp
        .Open "POST", "......", False
        .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send "Role=7&Account=" & txtUserName.Text & "&Password=......&CheckCode=" & txtCheck.Value
        URL = Split(Split(.responsetext, "CDATA[")(1), "]]")(0)
    End With
   
    On Error Resume Next
    Dim i As Long, j%, r
    i = ActiveSheet.UsedRange.Rows.Count
    With CreateObject("internetexplorer.application")
        .Visible = True
        .Navigate URL
        Do Until .ReadyState = 4
            DoEvents
        Loop
   
        .Navigate "......"
        Do Until .ReadyState = 4
            DoEvents
        Loop
        Set r = .document.all.tags("table")(0).Rows
        If Cells(i, 1) = r(0).Cells(1).innerText Then .Navigate URL: Exit Sub
        For j = 0 To r.Length - 1
            Cells(i + 1, j * 2 + 1) = "'" & r(j).Cells(1).innerText
            Cells(i + 1, j * 2 + 2) = "'" & r(j).Cells(3).innerText
        Next
        .Navigate URL
        .Quit
    End With
End If
End Sub

TA的精华主题

TA的得分主题

发表于 2016-11-26 13:41 | 显示全部楼层
VBA万岁 发表于 2015-2-2 08:44
奇怪,在另一台电脑上下载第10、11楼的附件,居然提示“有病毒”,遂贴出全部代码,看看是否有人帮忙诊 ...

窗体MainFrm代码:
验证码的获取用了一个winHttp,登录网址又用了一个winHttp,两个winHttp的Cookie不一样
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-10-6 17:21 , Processed in 0.030059 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表