首页 > 解决方案 > VBA 和 Excel:绕过受 Siteminder 保护的身份验证浏览网站

问题描述

我的目标:自动进入 Intranet 站点以获取 .xls 文件中的数据

初步解释:我定期通过手动操作在一天内大量获取数据;我需要一个自动解决方案来节省时间。可用手段:Excel 和 Vba(我公司建立的外部约束)

我所做的第一次尝试如下,执行登录操作(受 Siteminder 保护)并加入主页:

Sub enter_site()
Dim myId As String, myCode As String
Dim s3 As Worksheet
Dim IE As Object, frm As Object
Set s3 = ThisWorkbook.Worksheets("Sheet3")
myId = s3.Range("I1")      
myCode = s3.Range("K1")   
Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .navigate "https://intranetsite.com/up-password/LoginUp-Password.jsp?TARGET2=https://intranetsite.com/up-password/login/login_redirect.html"
        While .Busy Or .readyState <> 4: DoEvents: Wend             
      'LOGIN     
        Set frm = .document.forms(1)
        frm.Item("USER").Value = myId
        frm.Item("PASSWORD").Value = myCode
Application.Wait (Now + TimeValue("0:00:02"))
        frm.submit        
        While .Busy Or .readyState <> 4: DoEvents: Wend        
    End With

‘CHANNEL TOWARDS HOME PAGE
With IE
    .navigate "https://site2.intranet.com/mask/home.aspx"
    While .Busy Or .readyState <> 4: DoEvents: Wend
End With

到一定程度是可以的。我的意思是:我可以加入主页,但尽管我尝试了不同的策略,但我不能获取所需的数据,因为:

  1. 我不能像通常在其他情况下那样操纵“敌对”日历和下拉菜单;
  2. 我还应该在此过程中操纵“敌对”弹出窗口。

所以,在我看来,最好的解决方案是关于 MSXML2.XMLHTTP 或 WinHttp.WinHttpRequest.5.1 方法,或类似的东西。但不幸的是,我对它们并不熟悉。无论如何,我试图继续前进。我不是一个容易气馁的人,我已经使用 Fiddler 解析了操作。

因此,我发起了一个 Get 请求,如下所示:

Dim URL As String, strResponse As String
Dim objHTTP As Object
URL = https://intranetsite.com/up-password/LoginUp-Password.jsp?TARGET2=https://intranetsite.com/up-password/login/login_redirect.html
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    With objHTTP
        .Open "GET", URL, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send
        strResponse = .responseText
        Sheets(2).Range("A1") = strResponse
End With

然后,我利用此页面中的提示捕获了一些相关的 cookie:如何在 VBA 中设置和获取 JSESSIONID cookie? 我把它们放在 2 个单元格中:

Dim ck1 As String
Dim ck2 As String
        ck1 = Sheets(2).Range("B12")
        ck2 = Sheets(2).Range("B13")

假设以下值:ck1 = “JSESSIONID=blablabla” ck2 = “BIGipServerauth-=zzzzzzzzzzzzzzzzzzzzzz”</p>

此外,如上所述,我有:

myId = s3.Range("I1")      '<<< Username (johnsmith)
myCode = s3.Range("K1")   '<<< Password (london55)

现在,下一步是通过 post 请求执行登录操作。

在提琴手我读到:

POST https://intranetsite.com/siteminderagent/forms/login.fcc HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer:  https://intranetsite.com/up-password/LoginUp-Password.jsp?TARGET2=https://intranetsite.com/up-password/login/login_redirect.html _redirect.html
Accept-Language: it-IT
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: intranetsite.com
Content-Length: 181
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=…..; BIGipServerauth-……..; _WL_AUTHCOOKIE_JSESSIONID=…; SMSESSION=…

SMENC=UTF-8&target……..login_redirect.html&smauthreason=0&USER=johnsmith&PASSWORD=london55

我应该如何构建发布请求?
(我注意到我可能需要捕获其他 cookie:_WL_AUTHCOOKIE_JSESSIONID 和 SMSESSION)。
(我不知道 SMENC 到底是什么;无论如何,我可以使用存储在 USER 和 PASSWORD 中的变量轻松构建它)。

标签: excelvbaauthenticationwebsiteminder

解决方案


推荐阅读