ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] winHttp post登陆的问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2015-1-28 16:20 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Public Function winHttpGetResponse(postURL As String, PostData As String, Method As String, setCookies As String, setReferer As String) As String
    If Len(Trim(setCookies)) = 0 Then setCookies = "a:x," '//cookie为空则随便弄个cookie,不然报错
    Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1") '创建WinHttp.WinHttpRequest
    'MsgBox "到 这里"
    WinHttp.Open Method, postURL, True '同步接收数据
    WinHttp.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = &H3300 '非常重要(忽略错误)
     '其它请求头设置
    WinHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    WinHttp.setRequestHeader "Referer", setReferer '来路
    WinHttp.setRequestHeader "Cookie", setCookies
    WinHttp.setRequestHeader "Content-Length", Len(PostData)
    WinHttp.Option(4) = 13056 '忽略错误标志
    WinHttp.Option(6) = True '为 True 时,当请求页面重定向跳转时自动跳转,False 不自动跳转,截取服务端返回的302状态。
    WinHttp.Send PostData '' 开始发送
    WinHttp.WaitForResponse '等待请求
    'MsgBox WinHttp.Status '请求状态
    '得到返回文本(或者是其它)
   
    strText = WinHttp.getAllResponseHeaders
    Debug.Print strText
   winHttpGetResponse = WinHttp.ResponseText
  ' Debug.Print winHttpGetResponse
End Function


Dim URL As String           '参数:URL,post或get网址
Dim poDa As String          '参数:poDa,post内容
Dim Mth As String           '参数:Mth,方法,为"POST"或"GET"
Dim cooki As String         '参数:cooki,post或get带上的cookie
Dim strweb As String        '参数:strweb,post或get带上的cookie
Dim setReferer As String    '参数:setReferer,来路
URL = "file:///C:\Users\ADMINI~1\AppData\Local\Temp\%W@GJ$ACOF(TYDYECOKVDYB.pnghttp://bbss.shangdu.com/Login/checklogin.jsp"
poDa = "txtNAME=" & Text1 & "&txtPassword=" & Text2 & "&randomcode=" & Text3 & "&action=newlogin&loginby=0&logintype=0"
'Debug.Print poDa
Mth = "POST"
setReferer = "file:///C:\Users\ADMINI~1\AppData\Local\Temp\%W@GJ$ACOF(TYDYECOKVDYB.pnghttp://bbss.shangdu.com"

调用函数
Call winHttpGetResponse(URL, poDa, Mth, cooki, setReferer)
为什么登陆不了呢???
搞了一个下午都没有搞明白, 请各位大侠指点一下

用户名为:seo123www
密码:qazwsx123


TA的精华主题

TA的得分主题

发表于 2015-1-28 17:07 | 显示全部楼层
Try it if help you

  1. Option Explicit
  2. Const username As String = "seo123www"
  3. Const password As String = "qazwsx123"
  4. Sub test()
  5. Dim vcode As String     '验证码
  6. With CreateObject("winhttp.winhttprequest.5.1")
  7.     .Open "GET", "http://bbss.shangdu.com/Login/randomcode.jsp?r=" & Int(1000 * Rnd), False
  8.         .send
  9.     vcode = ""  '取出返回的验证码值,动态值,可自动识别,也可手动输入
  10.     .Open "POST", "http://bbss.shangdu.com/Login/login.jsp", False
  11.         .setrequestheader "Content-Type", "application/x-www-form-urlencoded"
  12.         .send "backurl=http%3A%2F%2Fbbss.shangdu.com%2FLogin%2Fchecklogin.jsp&action=newlogin&loginby=0&logintype=0&txtNAME=" & username & "&txtPassword=" & password & "&safecode=&randomcode=" & vcode & "&x=44&y=14"
  13.      If InStr(.responsetext, username) Then
  14.         MsgBox "登录成功"
  15.     Else
  16.         MsgBox "登录失败"
  17.     End If
  18. End With
  19. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2015-1-28 17:14 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-1-28 17:25 | 显示全部楼层
coby001 发表于 2015-1-28 17:14
&safecode=

你少了验证码~

&randomcode=

这个才是验证码

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-1-28 17:26 | 显示全部楼层
浮华、缠绕指尖 发表于 2015-1-28 17:07
Try it if help you

用你这个代码是提示登陆成功了,但是我用IE浏览器查看还是没有登陆。是什么情况呢,

TA的精华主题

TA的得分主题

发表于 2015-1-28 18:03 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
猎人seo 发表于 2015-1-28 17:26
用你这个代码是提示登陆成功了,但是我用IE浏览器查看还是没有登陆。是什么情况呢,

跟IE有什么关系,就好比你有二张银行卡,你从一张银行里取了钱,那你为什么另一张卡里的钱没有扣除呢?

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-1-28 21:08 | 显示全部楼层
浮华、缠绕指尖 发表于 2015-1-28 18:03
跟IE有什么关系,就好比你有二张银行卡,你从一张银行里取了钱,那你为什么另一张卡里的钱没有扣除呢?

Private Sub Command2_Click()

Const username As String = "seo123www"
Const password As String = "qazwsx123"

Dim vcode As String     '验证码
With CreateObject("winhttp.winhttprequest.5.1")
    .Open "GET", "http://bbss.shangdu.com/Login/randomcode.jsp?r=" & Int(1000 * Rnd), False
        .send
    vcode = Text3.Text  '取出返回的验证码值,动态值,可自动识别,也可手动输入
    .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=" & username & "&txtPassword=" & password & "&safecode=&randomcode=" & vcode & "&x=44&y=14"
     'Debug.Print .responsetext
     Debug.Print "backurl=http%3A%2F%2Fbbss.shangdu.com%2FLogin%2Fchecklogin.jsp&action=newlogin&loginby=0&logintype=0&txtNAME=" & username & "&txtPassword=" & password & "&safecode=&randomcode=" & vcode & "&x=44&y=14"
     If InStr(.responsetext, username) Then
        MsgBox "登录成功"
    Else
        MsgBox "登录失败"
    End If
End With


End Sub

把代码改成这样没错吧!!为什么显示登陆失败

TA的精华主题

TA的得分主题

发表于 2015-1-29 14:46 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-1-29 17:35 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
http://bbss.shangdu.com/Login/login.jsp

http://bbss.shangdu.com/Login/checklogin.jsp

http://bbss.shangdu.com/script/chklogin.php

从 login.jsp 到 chklogin.php ,中间有2次 302跳转,你都要模拟出来,并实时更新cookie,才能登陆。

TA的精华主题

TA的得分主题

发表于 2015-1-29 17:46 | 显示全部楼层
唉,还是扔个附件吧。。


商都登录BySuper.rar

14.73 KB, 下载次数: 268

评分

3

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-10-6 15:02 , Processed in 0.037806 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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